Skip to content

Commit ccee95f

Browse files
committed
Adicionando exemplo de fatorial
1 parent e37f31e commit ccee95f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# pycodes
2+
23
Códigos de Exemplo em Python
34

45
Repositório criado para hospedagem de códigos, testes e experimentos.
6+
7+
## Arquivo fatorial_pd
8+
9+
Modelo de fatorial

fatorial_pd.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import timeit
2+
import time
3+
4+
def fat(n):
5+
if n <= 1:
6+
return 1
7+
if not hasattr(fat, 'cache'):
8+
fat.cache = {}
9+
if n in fat.cache:
10+
return fat.cache[n]
11+
result = n * fat(n-1)
12+
fat.cache[n] = result
13+
return result
14+
15+
def exec2n(n):
16+
print(f'Fatorial de {n}')
17+
t1 = timeit.default_timer()
18+
res = fat(n)
19+
t2 = timeit.default_timer()
20+
print(' - Resultado:', res)
21+
print(' - Tempo:', round((t2-t1), 5))
22+
return res
23+
24+
25+
print('Rodando pela primeira vez:')
26+
exec2n(100)
27+
print()
28+
exec2n(102)
29+
print()
30+
print()
31+
32+
print('Rodando pela segunda vez:')
33+
exec2n(100)
34+
print()
35+
exec2n(102)

0 commit comments

Comments
 (0)