Last active
July 31, 2019 09:09
-
-
Save milosb793/378e03f9a4278eb1f9347e9eaafddd9e to your computer and use it in GitHub Desktop.
Recursive & Iterative solution of Factorial algorythm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
About: | |
Script for playing with recursion in python, by calculating factorial of number n | |
Supported versions: | |
>= 2.7 | |
Examples: | |
factorial.py 100 | |
factorial.py 100 -jt (display just seconds, without results) | |
Some of the results: | |
➜ python3.7 factorial.py 10 -jt | |
[0.00001s] Recursive | |
[0.00001s] Iterative | |
➜ python3.7 factorial.py 100 -jt | |
[0.00014s] Recursive | |
[0.00003s] Iterative | |
➜ python3.7 factorial.py 1000 -jt | |
[0.00275s] Recursive | |
[0.00090s] Iterative | |
➜ python3.7 factorial.py 10000 -jt | |
[0.14668s] Recursive -> recursion starting to get faster then iterative call on this point | |
[0.15047s] Iterative | |
... getting segmentation fault for 100000 ... | |
""" | |
import time, sys | |
from concurrent.futures import ThreadPoolExecutor | |
def _print(x): | |
try: | |
print(x) | |
except: | |
sys.stderr.write(x) | |
def get(l, index, default=None): | |
""" | |
Safe list accessor | |
""" | |
try: | |
return l[index] | |
except: | |
return default | |
def stopwatch(): | |
""" | |
Count how much time passes until fist call of this function | |
until next call | |
Example: | |
s = stopwatch() | |
[for i in range(20000)] | |
print(s("Done!")) | |
""" | |
start_time = time.time() | |
return lambda message: "[{:.5f}s] {}".format(time.time() - start_time, message) | |
# recursive lambda function | |
fac_recursive = lambda x: 1 if x <= 1 else x * fac_recursive(x-1) | |
def fac_iterative(x): | |
""" | |
Iterative factorial function | |
""" | |
if x <= 1: | |
return 1 | |
out = 1 | |
while x > 1: | |
x, out = x-1, out * x | |
return out | |
def calc_factorial_recursive(n): | |
""" | |
Calculate factorial recursively | |
""" | |
global just_time | |
s = stopwatch() | |
try: | |
result = fac_recursive(n) | |
except: | |
result = None | |
return s('Recursive' if just_time else "Recursive: {}\n\n".format(result)) | |
def calc_factorial_iterative(n): | |
""" | |
Calculate factorial iteratively | |
""" | |
global just_time | |
s = stopwatch() | |
result = fac_iterative(n) | |
return s('Iterative' if just_time else "Iterative: {}\n\n".format(result)) | |
def main(): | |
global just_time | |
# get args | |
n = int(get(sys.argv, 1, 0)) | |
just_time = get(sys.argv, 2, False) == '-jt' | |
# exit if no number passed | |
exit("factorial.py - Help\n\nExample: \n\tfactorial.py 10\n\tfactorial.py 10 -jt (to hide results)") if not n else '' | |
# execute in parallel | |
with ThreadPoolExecutor(max_workers=3) as executor: | |
recursive_future = executor.submit(calc_factorial_recursive, n) | |
iterative_future = executor.submit(calc_factorial_iterative, n) | |
_print(recursive_future.result()) | |
_print(iterative_future.result()) | |
if __name__ == '__main__': | |
# extend max recursion depth | |
sys.setrecursionlimit(100000) | |
just_time = False | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment