aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/python
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-18 02:28:15 +0100
committerAbigail <abigail@abigail.be>2021-03-18 02:28:15 +0100
commit88f893b7dfa2e8e240a7bc086c8a66215a9174b3 (patch)
tree6782f7609ca0c22fc28b898319a6b0854619eaab /challenge-104/abigail/python
parentaf98f5448ed09eab256501739b99fb17444b430a (diff)
downloadperlweeklychallenge-club-88f893b7dfa2e8e240a7bc086c8a66215a9174b3.tar.gz
perlweeklychallenge-club-88f893b7dfa2e8e240a7bc086c8a66215a9174b3.tar.bz2
perlweeklychallenge-club-88f893b7dfa2e8e240a7bc086c8a66215a9174b3.zip
Alternative Python solution for week 104, part 1
Diffstat (limited to 'challenge-104/abigail/python')
-rw-r--r--challenge-104/abigail/python/ch-1a.py41
1 files changed, 41 insertions, 0 deletions
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 ("")