Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/data/math/factorial
blob: 9db469c5b4e43ba791ba28028a13d8bfbd04146b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)