web-dev/hws/hw-2/fact.py
2026-02-13 15:01:19 +03:00

36 lines
845 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
def fact_rec(n):
if n < 1 or n >= 1000:
return "Error"
if n <= 1:
return 1
return n * fact_rec(n - 1)
def fact_it(n):
if n < 1 or n >= 1000:
return "Error"
result = 1
for i in range(2, n + 1):
result *= i
return result
if __name__ == '__main__':
n = 999
max_it = 0
for _ in range(1000):
start_1 = time.time()
fact_it(n)
it_time = time.time() - start_1
if max_it < it_time: max_it = it_time
max_rec = 0
for _ in range(1000):
start_2 = time.time()
fact_rec(n)
rec_time = time.time() - start_2
if max_rec < rec_time: max_rec = rec_time
print(f"Итеративная: {max_it}с") # 0.001142740249633789
print(f"Рекурсивная: {max_rec}с") # 0.0011646747589111328