I am trying to solve this 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.
Below is the code I have to calculate a fibonacci number:
# fibonacci series
def fib(x):
if x==0 or x==1:
return 1
else:
return fib(x-1) + fib(x-2)
def test_fib(n):
for i in range (n+1):
print 'fib of', i, ' = ' , fib(i)
test_fib(41)
However, the program hangs after 40th term. What is the problem with this code and how to solve this problem for 40th and beyond terms?