Home > ProjectEuler.Net, Python > Project Euler – Problem 2

Project Euler – Problem 2


Homepagehttp://projecteuler.net/problem=2

Problem:

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.

How I solve:
– In Python,


# Fibonacci
def Problem2():
	x, y = 0, 1
	sum = 0
	while True:
		x, y = y, x + y
		if y >= 4000000: break
		elif y % 2 == 0: sum += y
	print sum

Problem2()

Cheers,
Pete Houston

Advertisements
Categories: ProjectEuler.Net, Python Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: