Moved 1 to 5 to Python and learned a ton.
This commit is contained in:
24
python/e002.py
Normal file
24
python/e002.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from lib_fibonacci import fibonacci_generator_smaller
|
||||
|
||||
|
||||
def euler_002():
|
||||
fs = [f for f in fibonacci_generator_smaller(4000000) if f % 2 == 0]
|
||||
return sum(fs)
|
||||
|
||||
|
||||
assert(euler_002() == 4613732)
|
||||
print("e002.py: {}".format(euler_002()))
|
||||
|
||||
|
||||
def euler_002_simple():
|
||||
""" Sometimes it is nice to keep it simple. Instead of using three
|
||||
library functions just implement one straightforward function. """
|
||||
r, a, b = 0, 0, 1
|
||||
while b < 4000000:
|
||||
if b % 2 == 0:
|
||||
r += b
|
||||
a, b = b, a + b
|
||||
return r
|
||||
|
||||
|
||||
assert(euler_002() == euler_002_simple())
|
||||
Reference in New Issue
Block a user