diff options
| author | Abigail <abigail@abigail.be> | 2021-05-11 19:29:08 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-05-11 19:29:08 +0200 |
| commit | d498fd3ea3e1df8c647b83e5e4922e6af971960b (patch) | |
| tree | 819e85b07402a425620c67795fbac4b2619be967 /challenge-112/abigail/python | |
| parent | 6b21ba5c2303a59a3cca7b693ea0a55faf766c95 (diff) | |
| download | perlweeklychallenge-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.py | 13 |
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)) |
