[Project Euler] Exercise 02


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. """
  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:
  3.  
  4. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  5.  
  6. By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  7. """
  8.  
  9. l = [1,1]
  10. x = 1
  11. while (x <= 4000000):
  12. x = l[len(l)-1]+l[len(l)-2]
  13. l.append(x)
  14. print sum([i for i in l if(i%2==0)])

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.