diff options
| author | Abigail <abigail@abigail.be> | 2021-03-18 02:28:15 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-18 02:28:15 +0100 |
| commit | 88f893b7dfa2e8e240a7bc086c8a66215a9174b3 (patch) | |
| tree | 6782f7609ca0c22fc28b898319a6b0854619eaab | |
| parent | af98f5448ed09eab256501739b99fb17444b430a (diff) | |
| download | perlweeklychallenge-club-88f893b7dfa2e8e240a7bc086c8a66215a9174b3.tar.gz perlweeklychallenge-club-88f893b7dfa2e8e240a7bc086c8a66215a9174b3.tar.bz2 perlweeklychallenge-club-88f893b7dfa2e8e240a7bc086c8a66215a9174b3.zip | |
Alternative Python solution for week 104, part 1
| -rw-r--r-- | challenge-104/abigail/README.md | 4 | ||||
| -rw-r--r-- | challenge-104/abigail/python/ch-1a.py | 41 |
2 files changed, 44 insertions, 1 deletions
diff --git a/challenge-104/abigail/README.md b/challenge-104/abigail/README.md index 511fe961a0..7489ba54d5 100644 --- a/challenge-104/abigail/README.md +++ b/challenge-104/abigail/README.md @@ -53,7 +53,9 @@ the solution. * [Simple](perl/ch-1.pl) * [Calculated](perl/ch-1a.pl) * [PHP](php/ch-1.php) -* [Python](python/ch-1.py) +* Python + * [Simple](python/ch-1.py) + * [Calculated](python/ch-1a.py) * [R](r/ch-1.r) * [Rexx](rexx/ch-1.rexx) * [Ruby](ruby/ch-1.rb) diff --git a/challenge-104/abigail/python/ch-1a.py b/challenge-104/abigail/python/ch-1a.py new file mode 100644 index 0000000000..6eea537ff9 --- /dev/null +++ b/challenge-104/abigail/python/ch-1a.py @@ -0,0 +1,41 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1a.py +# + +# +# Initialize the cache +# +cache = {0: 0, 1: 1} +max = 50 + +# +# Fusc sequence is defined as: +# ( n, 0 <= n <= 1 +# fusc (n) = { fusc (n / 2), n > 1 && n even +# ( fusc ((n - 1) / 2) + fusc ((n + 1) / 2), n > 1 && n odd +# +def fusc (n): + if n not in cache: + if n % 2 == 1: + cache [n] = fusc ((n - 1) / 2) + fusc ((n + 1) / 2) + else: + cache [n] = fusc (n / 2) + + return cache [n] + + +# +# Calculate the first results of the fucs series, and print them +# +for i in range (max): + if i > 0: + print (' ', end = "") + print (fusc (i), end = "") + +print ("") |
