Vous êtes sur la page 1sur 1

Page: 1

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

import time
l = range(1,20)
#Factorielle avec memoization (programmation dynamique)
memo = {}
def factorielle (n):
if n == 1:
return 1
if not n in memo:
memo[n] = n*factorielle(n-1)
return memo[n]
print (factorielle(5))
tps1 = time.clock()
for i in l:
factorielle(i)
tps2 = time.clock()
print ("Temps d'xcution de factorielle memoize")
print (tps2-tps1)
print( memo)

def factorielle1(n):
if n== 1:
return 1
else:
return n* factorielle1(n-1)
tps3 = time.clock()
for i in l:
factorielle1(i)
tps4 = time.clock()
print ("Temps d'xcution de factorielle")
print (tps4-tps3)

Vous aimerez peut-être aussi