Even Fibonacci numbers
2014-09-11
Problem 002: Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Solution:
v>v>ppp 0>>$ 10g20g:10p+:v
202 >00p0^|-*2/2::< vp02<
0::0^+ g00<v**"}P ":<
:1>^@$.g00<>*` #v_^
>^ ^ <
202 >00p0^|-*2/2::< vp02<
0::0^+ g00<v**"}P ":<
:1>^@$.g00<>*` #v_^
>^ ^ <
Start
??
Pause
Reset
Output:
Stack: (0)
Explanation:
Also the problem here was not the algorithm, but writing and compressing it in Befunge. The code calculates the Fibonacci sequence one after each other and remembers the sum while iterating.
Interpreter steps: | 1 669 |
Execution time (BefunExec): | 62ms (0.03 MHz) |
Program size: | 26 x 5 (fully conform befunge-93) |
Solution: | 4613732 |
Solved at: | 2014-09-11 |