Commits

Ian Lewis committed a85b307

Added solution for problem 2

Comments (0)

Files changed (1)

+u"""
+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.
+"""
+
+MAX_FIB_VAL = 4000000
+
+def fibonacci():
+    minus_2, minus_1 = 0, 1
+    yield minus_2 
+    yield minus_1 
+    while True:
+        fib = minus_2 + minus_1
+        yield fib
+        minus_2, minus_1 = minus_1, fib 
+
+def euler2():
+    running_sum = 0
+    for fib in fibonacci():
+        if fib > MAX_FIB_VAL:
+            return running_sum
+        if fib % 2 == 0:
+            running_sum += fib
+
+if __name__ == '__main__':
+    print euler2()