fork download
  1. # your code goes here
  2. def soma():
  3. fat = -1
  4. somar = 0
  5. for i in range(1, 11):
  6. fat = -fat * i
  7. somar += (11 - i) / fat
  8. return round(somar, 2)
  9.  
  10. def fatorial(n):
  11. if n == 0 or n == 1:
  12. return 1
  13. return n * fatorial(n - 1)
  14.  
  15. def recursivo():
  16. num_dem = zip(reversed(range(1,11)), range(1,11))
  17. S = 0
  18. for num, dem in num_dem:
  19. fat = fatorial(dem)
  20. tmp = num/fat
  21. S += num/fat
  22.  
  23. from math import factorial
  24.  
  25. def somar(n):
  26. soma = cont = 0
  27. for c in range(n, 0, -1):
  28. cont += 1
  29. termo = (c / factorial(cont))
  30. if cont % 2 == 0:
  31. soma -= termo
  32. else:
  33. soma += termo
  34. return round(soma, 2)
  35.  
  36. from timeit import timeit
  37.  
  38. params = {'number': 200000, 'globals': globals()}
  39.  
  40. print(timeit('soma()', **params))
  41. print(timeit('somar(10)', **params))
  42. print(timeit('recursivo()', **params))
  43.  
Success #stdin #stdout 2.59s 9704KB
stdin
Standard input is empty
stdout
0.35921311751008034
0.5280378460884094
1.685017317533493