Commits

Tetsuya Morimoto  committed ce07b45

added an answer for problem 2

  • Participants
  • Parent commits 8a63dcc

Comments (0)

Files changed (1)

File python/2/even_values_of_fibonacci.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def fib(n):
+    """
+    >>> list(fib(50))
+    [1, 2, 3, 5, 8, 13, 21, 34]
+    """
+    a, b = 1, 2
+    while a < n:
+        yield a
+        a, b = b, a + b
+
+def sum_even_value(n):
+    """
+    >>> sum_even_value(50)
+    44
+    >>> sum_even_value(4000000)
+    4613732
+    """
+    answer = 0
+    for i in fib(n):
+        if i % 2 == 0:
+            answer += i
+    return answer