project euler problem #2

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]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s