Here is my Python code for Project Euler #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, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
How well did I do and what improvements can I make?
sum = 0
fib = []
i = 2
fib.append(0), fib.append(1), fib.append(1)
while fib[i] < 4000000:
i += 1
fib.append(fib[i-1] + fib[i-2])
if (fib[i] % 2 ==0):
sum += fib[i]
print sum
EDIT: Thanks everyone for answering and commenting. All answers and some comments provide different suggestions which is very helpful.
4000000
replaced with a variable) with paper and pencil with the right mathematical techniques. – Hurkyl Aug 31 at 20:47