Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was solving the Project Euler problem 2

*By starting with 1 and 2, the first 10 terms of Fibonacci Series 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 4 million.*

here is my code in Python

nums = []
while True:
for x in nums:
print sum

I noticed that the answer comes out to be 4613732 However I initially did a mistake by doing x%2!=0 and the answer turned out to be 4613731 (4613732-1) Is this some property or just luck??

share|cite|improve this question
up vote 4 down vote accepted

Each even valued term (which are the 2nd, 5th, and every 3rd one after) is the sum of the preceding two odd terms. There is a startup transient that 2 doesn't have two 1's before it (you would if you started with 1,1). So the sum of the even valued terms up to some value will be 1 more than the sum of the preceding odd terms.

share|cite|improve this answer

The sequence of Fibonacci numbers mod 2 goes $odd_1, odd_2, even_3$ - and in this sequence $odd_1+odd_2=even_3$. So the difference between the total of odds and evens up to any even number in the sequence is a constant, which happens to be 1. Stopping at an odd number gives a different result.

share|cite|improve this answer
+1 for the last line – nischayn22 Jun 18 '12 at 19:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.