cancel
Showing results for
Did you mean:

Q For Problems - Episode 2

New Contributor III

Q For Problems - Episode 2

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

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

Thanks

4 REPLIES 4
Moderator

Brilliant video @jkane71👏

Thank you for creating this series and sharing with us!

Looking forward to the next episode,

Leah

Contributor III

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[x]#100b}fibm 4000000
4613732``````

New Contributor III

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.

New Contributor III

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.5*1+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``````