Project Euler #2


Ok, I don’t intend to post all of them, but here’s the second problem:

# 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, …

# Find the sum of all the even-valued terms in the sequence which do not
# exceed four million.
def gen_Fib_even(a, b, below):
“””pre: a, b, below +ve integers
returns: generator with an iterator to obtain the Fibonacci sequence
“””
while (b < below): if b % 2 == 0: yield b c = b b = a + b a = c print sum(gen_Fib_even(1, 1, 4000000))[/sourcecode]

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s