princeofsudan

Project Euler #2 Even Fibonacci numbers

Mar 19th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. #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:
  2.  
  3. #1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
  4.  
  5. #Find the sum of all the even-valued terms in the sequence which do not exceed four million.
  6.  
  7. #Analysis
  8. #It can be demonstrated that every third term of the sequence is even.
  9.  
  10. limit = 4000000
  11. x, y, sum = 1, 1, 0
  12.  
  13. while (sum < limit):
  14.     sum += (x + y)
  15.     x, y = x + 2*y, 2*x + 3*y
  16.  
  17. print sum
Advertisement
Add Comment
Please, Sign In to add comment