ShareMyScreen/ProjectEuler/0002

From J Wiki
Jump to navigation Jump to search

The formula for the nth Fibonacci number (where fib(0)=0, fib(1)=1, etc.) is fib(n)=round(phin)/sqrt(5)) where phi is the golden ratio (1+sqrt(5))/2.

With this numbering, the first even Fibonacci number is fib(3). Clearly every third Fibonacci number is even; what is the last one less than 4 million? Solution:

   phi =. -: >: %: 5  NB. golden ratio
   phi ^. 4000000 * %: 5  NB. log (base phi) of 4 million times sqrt(5): number of fibs < 4e6
33.2629
   fib =. {{ <. 0.5 + (phi^y) % %: 5 }}  NB. fib number y
   +/ fib 3 * >: i. 11  NB. Add fibs 3, 6, 9, ... 33
xxxxxxx