#Project Euler 2 #Find sum of even Fibonacci numbers <4 million #function to return list of first n Fibonacci numbers def fibonacci(n): F = [1,1] #set initial values for i in range(2,n): F.append(F[i-1]+F[i-2]) return F #Find the index of the first Fibonacci number >4 million F=fibonacci(100) for x in F: if x > 4000000: N = F.index(x) break print sum( [ x for x in F[0:N] if x%2==0] ) #4613732 print ''
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question