aboutsummaryrefslogtreecommitdiff
path: root/challenge-112/abigail/python
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-11 19:29:08 +0200
committerAbigail <abigail@abigail.be>2021-05-11 19:29:08 +0200
commitd498fd3ea3e1df8c647b83e5e4922e6af971960b (patch)
tree819e85b07402a425620c67795fbac4b2619be967 /challenge-112/abigail/python
parent6b21ba5c2303a59a3cca7b693ea0a55faf766c95 (diff)
downloadperlweeklychallenge-club-d498fd3ea3e1df8c647b83e5e4922e6af971960b.tar.gz
perlweeklychallenge-club-d498fd3ea3e1df8c647b83e5e4922e6af971960b.tar.bz2
perlweeklychallenge-club-d498fd3ea3e1df8c647b83e5e4922e6af971960b.zip
Use closed form to calculate Fibonacci numbers for week 112, part 2.
Diffstat (limited to 'challenge-112/abigail/python')
-rw-r--r--challenge-112/abigail/python/ch-2.py13
1 files changed, 5 insertions, 8 deletions
diff --git a/challenge-112/abigail/python/ch-2.py b/challenge-112/abigail/python/ch-2.py
index 3e2a55750d..da542b06f2 100644
--- a/challenge-112/abigail/python/ch-2.py
+++ b/challenge-112/abigail/python/ch-2.py
@@ -5,17 +5,14 @@
#
#
-# Run as: python ch-1.py < input-file
+# Run as: python ch-2.py < input-file
#
import fileinput
+from math import sqrt
-cache = {0: 1, 1: 1}
-
-def fib (n):
- if not n in cache:
- cache [n] = fib (n - 1) + fib (n - 2)
- return (cache [n])
+SQRT5 = sqrt (5)
+PHI = (1 + SQRT5) / 2
for line in fileinput . input ():
- print (fib (int (line)))
+ print (round (pow (PHI, int (line) + 1) / SQRT5))