diff options
author | daedalus <daedalus2027@gmail.com> | 2009-08-31 00:19:05 (GMT) |
---|---|---|
committer | daedalus <daedalus2027@gmail.com> | 2009-08-31 00:19:05 (GMT) |
commit | ee552fcbdad3c3ab82934db4a0f51f17ea6cf579 (patch) | |
tree | bc7c2564ba192137e6f4c454ed2e0ca8763341c0 | |
parent | 0d622e6c2fe08c9d06ac03aa6d28e49b2f66da21 (diff) |
factorial calculation added
-rw-r--r-- | data/math/factorial | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/data/math/factorial b/data/math/factorial new file mode 100644 index 0000000..9db469c --- /dev/null +++ b/data/math/factorial @@ -0,0 +1,40 @@ +#importamos la biblioteca que contiene las funciones para medir el tiempo de ejecucion +import timeit +# inicializamos el timer +t = timeit.Timer() + +#definimos la funcion factorial recurrente +def factor_recurrente(n): + print "factorizando:",n + resultado = 1 + if n > 0: + resultado = n * factor_recurrente(n-1) + return resultado + +#definimos la funcion factorial iterativa +def factor_iterativo(n): + resultado = 1 + for i in range(1,n+1): + print "factorizando:",i + resultado = resultado * i + return resultado + +def calcular(n,tipo): + tiempo = t.timeit() + if tipo == 0: + tipo_s = "recurrente" + resultado = factor_recurrente(n) + else: + tipo_s = "iterativo" + resultado = factor_iterativo(n) + delta = abs(t.timeit() - tiempo) + print "factorial(",n,") = ", resultado, "tipo:" , tipo_s , " en: ", 1/delta + +#preguntamos el numero a factorizar +numero = input("Ingrese su numerito para calcular el factorial:") +print "Calculando..." +calcular(numero,0) +calcular(numero,1) + + + |