Q For Problems - Episode 2

[Q For Problems - Episode 2](“https://vimeo.com/756732509” "“Q”) 

 

Hi everyone,

 

Please check out episode 2 of the Q For Problems video series.

This covers problem 2 from the Project Euler problem set. [Project Euler - Problem 2](“https://projecteuler.net/problem=2” "“Project”) 

Feel free to share your own solutions and ideas in the comments.

 

Thanks

Brilliant video @jkane71

Thank you for creating this series and sharing with us!

Looking forward to the next episode, 

Leah

Sweet! And you can skip the arithmetic – every third Fibonacci number is even:

q)fib:{x,sum -2#x}/[;0 1] q)fibm:{-1 _ fib(x>last@)} q)fibm 4000000 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17.. q)sum{x where count#100b}fibm 4000000 4613732

 

Nice one! I was unaware of this property of the Fibonacci sequence, but after examining it makes sense because adding two odds will always produce an even, and adding an even and an odd always produces an odd, so the sequence of (even, odd, odd) will repeat every forever.

It’s also possible to calculate the n-th Fibonacci number directly using the golden ratio, phi. So for this case, we can just calculate every 3rd number since we know they are even.

q)phi:0.51+sqrt 5 q)fibn:{reciprocal[sqrt 5](-/)xexp[;y]x,1-x}[phi;] q)sum -1_{4000000>last x}{x,fibn 3*count x}/0f 4613732f