diff options
Diffstat (limited to 'algorithms/factorial.py')
-rwxr-xr-x | algorithms/factorial.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/algorithms/factorial.py b/algorithms/factorial.py new file mode 100755 index 0000000..41eb708 --- /dev/null +++ b/algorithms/factorial.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python2 + + +cache = {0:1} + +def fak(n): + if n == 0: return 1 + if cache.get(n, 0): + return cache[n] + if not cache.get(n-1, 0): + cache[n-1] = fak(n-1) + return n*cache[n-1] + +def fakn(n): + if n == 0: + return 1 + else: + return n*fak(n-1) + +for i in [fak(i) for i in reversed(range(10))]: + print i + |