# euler

git clone git://git.codymlewis.com/euler.git
Log | Files | Refs

```commit d4b5c67c0367f0f779ccb79f3ea0138ea68e7987
parent 89d0d1615d7a9178c8c65563124cd08b168b2cc9
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Sat, 17 Nov 2018 19:35:30 +1100

Solution to problem 2

Diffstat:
ATwo.py | 27+++++++++++++++++++++++++++
```
```1 file changed, 27 insertions(+), 0 deletions(-)
diff --git a/Two.py b/Two.py
@@ -0,0 +1,27 @@
+'''
+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.
+'''
+def find_sum(n):
+    fib = [1, 2, 0]  # Only ever need to store 3 Fibonacci numbers
+    sigma = 0
+    index = 1
+    while fib[index] < n:
+        if index == 1:  # Even number at this index every time
+            sigma += fib[index]
+        index = (index + 1) % 3  # Stop recalculation
+        fib[index] = fib[(index - 1) % 3] + fib[(index - 2) % 3]
+    return sigma
+
+
+if __name__ == "__main__":
+    LIMIT = 4_000_000
+    print(f"The sum of the Fibonacci numbers up not exceeding {LIMIT} is " +
+          f"{find_sum(LIMIT)}")
+
```