# sum of even-valued and odd-valued Fibonacci numbers

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

````a=0
b=1
nums = []
while True:
    x=a+b
    a=b
    b=x
    if(b>4000000):
        break
    nums.append(x)
sum=0
for x in nums:
    if(x%2==0):
        sum+=x
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??

-

## 2 Answers

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.

-

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.

-

+1 for the last line – nischayn22 Jun 18 '12 at 19:09
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 2, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9241681694984436, "perplexity_flag": "middle"}