aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/lua/ch-1a.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-104/abigail/lua/ch-1a.lua')
-rw-r--r--challenge-104/abigail/lua/ch-1a.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-104/abigail/lua/ch-1a.lua b/challenge-104/abigail/lua/ch-1a.lua
new file mode 100644
index 0000000000..16569bf822
--- /dev/null
+++ b/challenge-104/abigail/lua/ch-1a.lua
@@ -0,0 +1,49 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1a.lua
+--
+
+--
+-- Initialize the cache
+--
+
+local cache = {}
+cache [0] = 0
+cache [1] = 1
+local 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
+--
+
+function fusc (n)
+ if cache [n] == nil then
+ if n % 2 == 1 then
+ cache [n] = fusc ((n - 1) / 2) + fusc ((n + 1) / 2)
+ else
+ cache [n] = fusc (n / 2)
+ end
+ end
+ return cache [n]
+end
+
+
+--
+-- Calculate the values and print them
+--
+
+for i = 0, max - 1 do
+ if i > 0
+ then io . write (" ")
+ end
+ io . write (fusc (i))
+end
+io . write ("\n")