Created
May 16, 2016 04:10
-
-
Save cganterh/63ac364adc544a6c61670444948e6c77 to your computer and use it in GitHub Desktop.
Calcular el factorial de un número
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 python | |
# -*- coding: UTF-8 -*- | |
n = int( | |
raw_input('ingrese un numero: ') | |
) | |
m = 1 | |
for i in range(n): | |
m *= i + 1 | |
print m |
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 python | |
# -*- coding: UTF-8 -*- | |
def factorial(n): | |
m = 1 | |
for i in range(n): | |
m *= i + 1 | |
return m | |
n = int( | |
raw_input('ingrese un numero: ') | |
) | |
print factorial(n) |
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 | |
# -*- coding: UTF-8 -*- | |
from functools import reduce | |
def factorial(n): | |
try: | |
if n < 0: | |
raise ValueError( | |
'n has to be a positive integer') | |
return reduce(lambda a, b: a*b, range(1, n+1), 1) | |
except TypeError as te: | |
if not isinstance(n, int): | |
raise TypeError('n must be an integer') from te | |
else: | |
raise | |
if __name__ == '__main__': | |
n = int( | |
input('ingrese un numero: ') | |
) | |
print( | |
factorial(n) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment