Back to Home

Fibonacci even numbers

algorithms · fibonacci numbers · job interview · php · comments on a geek magazine

Fibonacci even numbers

Inspired by a comment under a Fibonacci post for an interview . User pavellyzhin mentioned the following interview task ( comment ):
More than a year ago, “php-programmer” responded to the vacancy, they sent TK and there was a task from Fibonacci: select all even Fibonacci numbers in the range from 1 to 10000 . Decided using a (for) loop. There, it was also necessary to make an SQL query to select the nearest birthdays of users, to make up something, I just don’t remember and write some kind of function. I did everything, sent it. They sent an answer: "according to the results of the test task you are not accepted." What exactly they did not like was not written. Now I'm sitting and thinking, probably after all because of Fibonacci I flew ... :)
In this post I am going to show how it was possible to solve this problem effectively, and maybe even efficiently, but this is not accurate. At the same time I will demonstrate a couple of thousands of facts proved about Fibonacci numbers.


Theorizing



The best way to start is to look through the eyes of the first N Fibonacci numbers and try to find a pattern in the arrangement of even numbers.

$ 1.1, {\ bf 2}, 3.5, {\ bf 8}, 13.21, {\ bf 34}, 55.89, {\ bf 144}, \ ldots $


Even numbers are marked in the sequence, as you can see every 3rd Fibonacci number is even and, probably, all even numbers are in positions of multiples of 3. This will be our guess, now we need to confirm it and work out a calculation algorithm.

The best proof is simple, so thanks to AllexIn for the simple idea that I initially missed. Each subsequent Fibonacci number is the sum of the previous two, if the two previous numbers are odd, then the next will be even, if in the two previous numbers one is odd and the other is even, then the next will be odd. In principle, this idea alone is already enough to “intuitively grope” the proven fact. The proof by induction is obvious, but I can not resist, so as not to bring it

We prove that "every third Fibonacci number is even, and the two preceding each such number are odd."
  • Base induction . First two fibonacci numbers$ 1.1 $ - odd, third number $ 2 $ - even
  • Hypothesis . Suppose that up to some Fibonacci number multiple of 3 it is true that every third is even, and the two preceding it are odd.
  • Induction step . The number following the last even one from the hypothesis is odd, because it is obtained from the sum of the odd with the even, following that number already is also odd, because it is obtained from the sum of the even with the odd, and the next after it is even, because the two previous ones just received for him are odd, by construction his number is a multiple of 3, it is even, and the two previous ones are odd.


This is how we can prove our guess without even resorting to mathematical calculations. You can formalize this idea, at the same time obtaining a formula for calculating every third Fibonacci number$ F_ {n + 3} $ of $ F_n $ and $ F_ {n + 1} $. Using the recursive relation$ F_ {n + 2} = F_ {n + 1} + F_ {n} $ we get:

$ F_ {n + 3} = F_ {n + 2} + F_ {n + 1} = 2 F_ {n + 1} + F_n $


So if $ F_n $ - even then $ F_ {n + 3} $also even by virtue of this formula. Given that$ F_3 = 2 $, then every third Fibonacci number is really even, which confirms part of our guess. Here you need to make sure that we do not miss other even numbers, i.e. that they all have multiples of 3. Thanks to janatem for his simple idea, because from the above formula for$ F_ {n + 3} $ it also follows that if $ F_n $ - odd then $ F_ {n + 3} $ also odd, so we get that numbers with numbers $ 3k-2,3k-1, k \ in \ mathbb {N} $ - odd (by induction, start with $ F_1 = F_2 = 1 $ - odd), and $ 3k, k \ in \ mathbb {N} $- even, which covers all the Fibonacci numbers, which means that we really cover all the even numbers.

There is another way, as it would be possible to show that there are no other even numbers. Suppose there exists a number$ F_m $ - even and $ 0 \ not \ equiv m \ mod 3 $then $ m = 3k - 1 $ or $ m = 3k + 1 $where $ k $- some kind of natural.

Let us turn to the matrix representation of Fibonacci numbers from the original post

$ \ begin {pmatrix} 0 & 1 \\ 1 & 1 \ end {pmatrix} ^ n = \ begin {pmatrix} F_ {n-1} & F_n \\ F_n & F_ {n + 1} \ end {pmatrix} $


Calculating the determinant of both parts, we obtain

$ (-1) ^ n = F_ {n + 1} F_ {n-1} -F_n ^ 2 $


It follows that the divisors of numbers $ F_ {n + 1}, F_n $ and $ F_ {n-1}, F_n $ match dividers $ (- 1) ^ n $, i.e. neighboring Fibonacci numbers are mutually simple. This also means that mutual simplicity and numbers$ F_ {3k}, F_ {3k-1} $ and $ F_ {3k}, F_ {3k + 1} $, i.e. $ F_ {3k} $ and $ F_m $. But by assumption$ F_m $ - even, and $ F_ {3k} $- even as previously proven. So other even numbers than$ F_ {3k} $where $ k \ in \ mathbb {N} $in the Fibonacci sequence does not exist. And also we established an interesting fact that the neighboring Fibonacci numbers are mutual simple.

Finally, we show at least one more way to prove our guess: use Luke's theorem.

Luke's theorem . Integer divides$ F_m $, and $ F_n $, if and only if it is a divisor $ F_d $where $ d = \ gcd (m, n) $, in particular

$ \ gcd (F_m, F_n) = F _ {\ gcd (m, n)} $


Here $ \ gcd $Is the largest common factor. If put$ m = 3 $then $ \ gcd (2, F_n) = F _ {\ gcd (3, n)} $. If$ F_n $ - even, then the left side is 2, so that the right side also equals 2, it is necessary that $ n $divided by 3 and at the same time the opposite is true. Thus, we get exactly what we wanted to prove.

So, every third Fibonacci number is even, and every even one has a multiple of three. We have carefully proved this and no one dares to doubt it now.

Algorithm


And now it remains to come up with an algorithm. You can of course do what pavellyzhin originally did , but instead of checking$ 0 \ equiv F_n \ mod 2 $we can check $ 0 \ equiv n \ mod 3 $, What a twist! True, this does not affect the number of iterations of the algorithm, because we just changed the sequence filter. It’s better to immediately generate a subsequence of Fibonacci numbers with the property we need (parity), so another obvious way is to use the Binet formula

$ F_n = \ left \ lfloor \ frac {\ varphi ^ n} {\ sqrt {5}} \ right \ rceil, \ qquad n \ in \ {3,6, \ ldots, 3k \ ldots \} $


There are difficulties with the efficiency of calculations, more about this in the original post. Therefore, I propose the best, in my opinion, method - iterative calculation$ F_ {n + 3} $, this can be done, as we previously showed, by the formula $ F_ {n + 3} = 2F_ {n + 1} + F_n $. To construct an iterative transition in the algorithm, we need to calculate$ F_ {n + 4} $, everything is just as simple

$ F_ {n + 4} = F_ {n + 3} + F_ {n + 2} = 2F_ {n + 1} + F_n + F_ {n + 1} + F_n = 3F_ {n + 1} + 2F_n $


By the way, generally speaking, it is easy to prove that

$ F_ {n + m} = F_mF_ {n + 1} + F_ {m-1} F_n $


Then formally the algorithm is written as follows (the current even Fibonacci number $ F_n $followed by the Fibonacci number $ F_ {n + 1} $, $ M $ Is the upper bound for numbers given in the condition of the problem):

  1. $ F_n \ leftarrow F_3 = 2, \ quad F_ {n + 1} \ leftarrow F_4 = 3 $
  2. If $ F_n> M $, then finish, otherwise add to the result $ F_n $
  3. $ (F_n, F_ {n + 1}) \ leftarrow (2F_ {n + 1} + F_n, 3F_ {n + 1} + 2F_n) $go to step 2.

The algorithm is quite trivial - we simply jump onto every third Fibonacci number and print it (if it is not more $ M $) The complexity of the algorithm is still linear, but the number of repetitions of step 2 is exactly equal to the number of even Fibonacci numbers in the range$ 1 \ ldots M $, for comparison, a simple algorithm with filtering requires 3 times more iterations (for each found, there will be 2 discarded).

The algorithm exists on paper, what was the interview, PHP? Well, finally uncover PHP means

function evenfibs($ubound) {
  $result = [];
  [$evenf, $nextf] = [2, 3];
  while($evenf <= $ubound) {
    array_push($result, $evenf);
    [$nextf, $evenf] = [
      3 * $nextf + 2 * $evenf,
      2 * $nextf + $evenf];
   }
  return $result;
}


Note : This method can always be improved, as, for example, the user hunroll showed . The idea is that for calculations we do not need anything superfluous, except for the partially obtained result, i.e. we can calculate even numbers using only the previous even Fibonacci numbers.

$ F_ {n + 3} = 2F_ {n + 1} + F_ {n} = 3F_n + 2F_ {n-1} = 3F_n + F_ {n-1} + F_ {n-1} = \\ 3F_n + ( F_ {n-1} + F_ {n-2}) + F_ {n-3} = 4F_n + F_ {n-3} $



function evenfibs($ubound) {
  if($ubound < 2) return [];
  $evens = [2];
  $next = 8;
  for($i = 1; $next <= $ubound; $i++) {
    $evens[$i] = $next;
    $next = $evens[$i]*4 + $evens[$i-1];
  }
  return $evens;
}


Generalization


We mentioned here Luke's theorem, which states that $ \ gcd (F_m, F_n) = F _ {\ gcd (m, n)} $. As a consequence of it, we can get that the Fibonacci number$ F_n $ multiple of $ F_m $, if and only if its number $ n $ multiple to number $ m $. Those. every 4th Fibonacci number is divided by 3, every 5th by 5, every 6th by 8, etc.

Then, in a simple way, we obtain an algorithm for calculating the Fibonacci subsequence, in which the elements are multiples of some given number$ F_m $. Using the fact that

$ F_ {n + m} = F_mF_ {n + 1} + F_ {m-1} F_n \\ F_ {n + m + 1} = F_ {m + 1} F_ {n + 1} + F_mF_n $


The previous algorithm turns into

  1. $ F_n \ leftarrow F_m, \ quad F_ {n + 1} \ leftarrow F_ {m + 1} $
  2. If $ F_n> M $, then finish, otherwise add to the result $ F_n $
  3. $ (F_n, F_ {n + 1}) \ leftarrow (F_mF_ {n + 1} + F_ {m-1} F_n, F_ {m + 1} F_ {n + 1} + F_mF_n) $go to step 2.

Where $ F_ {m-1}, F_m, F_ {m + 1} $can be set as constants.

Summary of notes . As the user ignorance rightly noted , in the generalized case, it is also possible to construct an algorithm that uses only numbers from a partial result.

$ F_ {n + 1} = F_n + F_ {n-1} \\ F_ {n + 2} = 3F_ {n} - F_ {n-2} \\ F_ {n + 3} = 4F_ {n} + F_ {n-3} \\ F_ {n + 4} = 7F_ {n} - F_ {n-4} \\ F_ {n + 5} = 11F_ {n} + F_ {n-5} \\ \ cdots \\ F_ {n + t} = (F_t + 2F_ {t-1}) F_n + (-1) ^ {t-1} F_ {nt} $



Let us prove this by the method of mathematical induction, for t = 1 and t = 2, the fulfillment is obvious. Suppose that it holds up to some t; then

$ F_{n+t+1}=F_{n+t} + F_{n+t-1}=\\ (F_t + 2F_{t-1})F_n + (-1)^{t-1}F_{n-t} + \\ (F_{t-1} + 2F_{t-2})F_n + (-1)^{t-2}F_{n-t+1} = \\ (F_t + F_{t-1} + 2(F_{t-1}+F_{t-2}))F_n + (-1)^{t-1}(F_{n-t}-F_{n-t+1})=\\ (F_{t+1}+2F_t)F_n + (-1)^{t-1}(F_{n-t}-F_{n-t}-F_{n-t-1})=\\ (F_{t+1}+2F_t)F_n + (-1)^tF_{n-t-1} $



Something like a total


The task of course is completely synthetic, the number of iterations is very small (for $M=10000$the answer contains only 6 numbers, i.e. 6 iterations would have been completed, and the original “head-on” algorithm would have required 18), but in this way a user who discovered something new here can now show a little deeper mathematical knowledge in Fibonacci numbers at the interview.

Edit: Thanks to janatem and AllexIn users for the simple proofs, I included them in “Theoremizing”, as well as hunroll for the algorithm using only the even numbers already obtained in the calculations and to the user ignorance for generalizing it.

Read Next