aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-104/abigail/lua')
-rw-r--r--challenge-104/abigail/lua/ch-1.lua12
-rw-r--r--challenge-104/abigail/lua/ch-1a.lua49
-rw-r--r--challenge-104/abigail/lua/ch-2.lua35
3 files changed, 96 insertions, 0 deletions
diff --git a/challenge-104/abigail/lua/ch-1.lua b/challenge-104/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..52af3c7d45
--- /dev/null
+++ b/challenge-104/abigail/lua/ch-1.lua
@@ -0,0 +1,12 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua
+--
+
+print ("0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 " ..
+ "5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9")
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")
diff --git a/challenge-104/abigail/lua/ch-2.lua b/challenge-104/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..70d1a3905d
--- /dev/null
+++ b/challenge-104/abigail/lua/ch-2.lua
@@ -0,0 +1,35 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua
+--
+
+local tokens = 12
+local max_take = 3
+
+while tokens > 0
+do local s = ""
+ if not (tokens == 1)
+ then s = "s"
+ end
+ io . write ("How many tokens do you take? ")
+ io . write (string . format ("(%2d token%s are left) ", tokens, s))
+
+ local take = tonumber (io . read ("*l"))
+
+ if take and 1 <= take and take <= max_take
+ then local takes = max_take + 1 - take
+ local s = ""
+ if not (takes == 1)
+ then s = "s"
+ end
+ io . write (string . format ("Computer takes %d token%s\n", takes, s))
+ tokens = tokens - (max_take + 1)
+ end
+end
+
+io . write ("Computer wins\n")