Last active
August 29, 2015 13:56
-
-
Save Ismael-VC/9164420 to your computer and use it in GitHub Desktop.
Sum even factorials.
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
#!/usr/bin/env python3 | |
def factorial(n): | |
""" | |
>>> factorial(20) | |
2432902008176640000 | |
""" | |
if n < 2: | |
return 1 | |
else: | |
return n*factorial(n-1) | |
def sum_even_factorials(r): | |
""" | |
>>> sum_even_factorials(20) | |
6423384156578667 | |
""" | |
even_factorials = [factorial(n) for n in range(r) if n % 2 == 0] | |
return sum(even_factorials) | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod(verbose=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment