summaryrefslogtreecommitdiff
path: root/algorithms/factorial.py
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-14 16:46:27 +0200
committerPatrick Simianer <p@simianer.de>2014-06-14 16:46:27 +0200
commit26c490f404731d053a6205719b6246502c07b449 (patch)
tree3aa721098f1251dfbf2249ecd2736434c13b1d48 /algorithms/factorial.py
init
Diffstat (limited to 'algorithms/factorial.py')
-rwxr-xr-xalgorithms/factorial.py22
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
+