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.
Still a "simple" problem, probably simpler than the first one:
1) By the heart of brute-force:
Test every fibonacci numbers if they are even, sum the results.
2) Observe the pattern that every 3rd fib-numbers are even:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Just modify the program into generate only every thirds fib-numbers.
3) Observe the pattern of only even fib-numbers:
2 8 34 144... which is E(n)=4*E(n-1)+E(n-2)
It's a good try if you try to prove this pattern by your own.
Though you can cheat by looking at the official overview PDF, XD
P.S. As we are generating all the fib numbers in the sequence, using iteration(or tail recursion) is essentially much better than simple recursion.
[GitHub Link] [Official overview pdf by projecteuler.net]
1) By the heart of brute-force:
Test every fibonacci numbers if they are even, sum the results.
2) Observe the pattern that every 3rd fib-numbers are even:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Just modify the program into generate only every thirds fib-numbers.
3) Observe the pattern of only even fib-numbers:
2 8 34 144... which is E(n)=4*E(n-1)+E(n-2)
It's a good try if you try to prove this pattern by your own.
Though you can cheat by looking at the official overview PDF, XD
P.S. As we are generating all the fib numbers in the sequence, using iteration(or tail recursion) is essentially much better than simple recursion.
[GitHub Link] [Official overview pdf by projecteuler.net]