diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-14 16:46:27 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-14 16:46:27 +0200 |
commit | 26c490f404731d053a6205719b6246502c07b449 (patch) | |
tree | 3aa721098f1251dfbf2249ecd2736434c13b1d48 /algorithms/factorial.py |
init
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 + |