Last active
December 26, 2015 03:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used the generator directly in the list comprehension and move the logic of limiting the length of fibonacci series to the generator.