Skip to content

Instantly share code, notes, and snippets.

@julius-datajunkie
Last active December 26, 2015 03:19
Show Gist options
  • Save julius-datajunkie/7085281 to your computer and use it in GitHub Desktop.
Save julius-datajunkie/7085281 to your computer and use it in GitHub Desktop.
Will try to implement the second order difference equation formula for this problem.
import unittest
def EvenFibonacciNumbers(limit):
#Find all even fibonacci numbers
fibonacciSeries = FibonacciGenerator(limit)
return sum(i for i in fibonacciSeries if i % 2 == 0)
def FibonacciGenerator(limit):
a = 0
b = 1
yield a
while True:
tmp = a+b
yield tmp
a = b
b = tmp
if tmp > limit:
break
class Test(unittest.TestCase):
def test_EvenFibonacciNumbers(self):
self.assertEqual(EvenFibonacciNumbers(10),10)
if __name__ == '__main__':
unittest.main(exit=False)
print(EvenFibonacciNumbers(4000000))
@julius-datajunkie
Copy link
Author

Used the generator directly in the list comprehension and move the logic of limiting the length of fibonacci series to the generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment