Skip to content

Instantly share code, notes, and snippets.

@cganterh
Created May 16, 2016 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cganterh/63ac364adc544a6c61670444948e6c77 to your computer and use it in GitHub Desktop.
Save cganterh/63ac364adc544a6c61670444948e6c77 to your computer and use it in GitHub Desktop.
Calcular el factorial de un número
#! /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
#! /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)
#! /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