Euler Project # 2 in Python

Background

I am stuck in this problem:

Each new member of the Fibonacci sequence is generated by adding the two previous members. Starting from 1 and 2, the first 10 members will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Considering the members in the Fibonacci sequence, whose values ​​do not exceed four million, we find the sum of the even members.

I tried to find out if the problem was my Fibonacci number generator, code that gets even numbers, or even the way I add numbers to no avail.

the code

I decided to keep the numbers on the lists. Here I create them.

list_of_numbers = [] #Holds all the fibs
even_fibs = [] #Holds only even fibs

Then I created a generator. This is a potential area of ​​concern .

x,y = 0,1 #sets x to 0, y to 1
while x+y <= 4000000: #Gets numbers till 4 million
    list_of_numbers.append(y)
    x, y = y, x+y #updates the fib sequence

Then I created code to check the parity of the number, and then add it to the list even_fibs. This is another weak point in the code.

coord = 0
for number in range(len(list_of_numbers)):
    test_number = list_of_numbers [coord]

    if (test_number % 2) == 0:
        even_fibs.append(test_number)
    coord+=1

, .

print "Normal:  ", list_of_numbers #outputs full sequence
print "\nEven Numbers: ", even_fibs #outputs even numbers
print "\nSum of Even Numbers:  ", sum(even_fibs) #outputs the sum of even numbers

, , ? , - .

+5
1

, 4 000 000. 4 000 000.

+4

All Articles