From 88f893b7dfa2e8e240a7bc086c8a66215a9174b3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 18 Mar 2021 02:28:15 +0100 Subject: Alternative Python solution for week 104, part 1 --- challenge-104/abigail/python/ch-1a.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-104/abigail/python/ch-1a.py (limited to 'challenge-104/abigail/python') 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 ("") -- cgit