Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--data/math/factorial40
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)
+
+
+