fork download
  1. # Factorial using the methods
  2. # - Divide Et Impera
  3. # - Recursive
  4. # - Iterative
  5. # n = 5
  6. # n! = 5!
  7. # fact = 1 * 2 * 3 * 4 * 5
  8.  
  9. def fact_dei(lo, hi):
  10. if lo == hi:
  11. return lo
  12. else:
  13. m = (lo + hi) >> 1
  14. a = fact_dei(lo, m)
  15. b = fact_dei(m+1, hi)
  16. return a * b
  17.  
  18. def fact_rec(n):
  19. if n == 0:
  20. return 1
  21. else:
  22. return n * fact_rec(n - 1)
  23.  
  24. def fact_it(n):
  25. p = 1
  26. for i in range(1, n + 1):
  27. p *= i
  28. return p
  29.  
  30. def main():
  31. n = 10
  32. res = fact_dei(1, n)
  33. print(res, end =' < -- > ')
  34. print(fact_rec(n), end =' < -- > ')
  35. print(fact_it(n), end = '')
  36. main()
Success #stdin #stdout 0.02s 9064KB
stdin
Standard input is empty
stdout
3628800 < -- > 3628800 < -- > 3628800