Created
February 1, 2012 15:49
-
-
Save erkobridee/1717651 to your computer and use it in GitHub Desktop.
Algoritmo matemático para calcular fatorial (com limite) e combinação
This file contains hidden or 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
def fatorial(n, limite=1): | |
if n == 0 or n == 1: | |
return 1 #por definição 0! ou 1! = 1 | |
if limite < 1 or limite >= n: | |
return -1 #representando valor incorreto | |
else : | |
fatArr = range(limite,n+1) | |
resultado = 1 | |
for i in fatArr: | |
resultado = resultado * i | |
return resultado | |
''' | |
Cn,p = n! / ( p! * (n-p)! ) | |
np = n - p | |
if np < p | |
limite = (n - p)+1 | |
else | |
limite = p + 1 | |
a = ( n, limite )! | |
b = ( p! * np! ) | |
Cn,p = a/b | |
''' | |
def combinacao(n,p): | |
if p > n: | |
return -1 | |
a = b = limite = 1 | |
np = n - p | |
if np < p: | |
limite = np+1 | |
b = p | |
else: | |
limite = p+1 | |
b = np | |
a = fatorial(n,limite) | |
b = fatorial(b) | |
return a/b | |
''' | |
C 60,9 = 60! / 9! * (60-9)! | |
C 60,9 = 60! / 9! * 51! | |
C 60,9 = 60*59*58*57*56*55*54*53*52*51! / 9! * 51! | |
C 60,9 = 60*59*58*57*56*55*54*53*52 / 9! | |
C 60,9 = 60*59*58*57*56*55*54*53*52 / 9*8*7*6*5*4*3*2*1 | |
C 60,9 = 5364506808460800 / 362880 | |
C 60,9 = 14783142660 | |
''' | |
print fatorial(60,52) | |
print fatorial(9) | |
print ( 60*59*58*57*56*55*54*53*52 ) / ( 9*8*7*6*5*4*3*2*1 ) | |
print combinacao(60,9) | |
''' | |
C8,7 = 8! / 7! * (8-7)! | |
C8,7 = 8! / 7! * 1! | |
C8,7 = 8*7! / 7! * 1! | |
C8,7 = 8 / 1 | |
C8,7 = 8 | |
''' | |
print combinacao(8,7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment