project euler problem #2

Aside

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.

http://projecteuler.net/problem=2


  • try solving it as a python one-liner – i couldn’t figure out a clean solution
  • try optimizing your solution for speed

highlight below for my solution:


#using a fibonacci dictionary - https://gist.github.com/aausch/6707846
fib_dict = FibDict()
j = 3
while (fib_dict[j] < 4000000):
    j = j + 3
print sum([fib_dict[i] for i in range(3,j,3)])  # j = 36, probably


[More programming riddles]