From 26c490f404731d053a6205719b6246502c07b449 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 14 Jun 2014 16:46:27 +0200 Subject: init --- algorithms/factorial.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 algorithms/factorial.py (limited to 'algorithms/factorial.py') 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 + -- cgit v1.2.3