project-euler-100

[RADIOACTIVE] solutions to the first 100 challenges of project euler
git clone git://git.figbert.com/project-euler-100.git
Log | Files | Refs | README

problem002.py (745B)


      1 # problemName = "Even Fibonacci numbers"
      2 # problemNum = 2
      3 # solutionBy = "FIGBERT"
      4 # language = "Python"
      5 # dateCompleted = "20/01/2020"
      6 
      7 def fibonacci(limit):
      8     list_ = [1,2]
      9     while list_[-1] <= limit:
     10         list_.append(list_[-2] + list_[-1])
     11     return list_
     12 
     13 def sum_of_evens_in_list(src_list):
     14     list_ = []
     15     for i in src_list:
     16         if i % 2 == 0:
     17             list_.append(i)
     18     return sum(list_)
     19 
     20 if __name__ == "__main__":
     21     fibonacci_until_4_mil = fibonacci(4000000)
     22     answer = sum_of_evens_in_list(fibonacci_until_4_mil)
     23     print((
     24         "By considering the terms in the Fibonacci sequence whose values do "
     25         "not exceed four million, the sum of the even-valued terms is "
     26         "{}".format(answer)
     27     ))