blob: 16569bf822b9d0a4c5f31e76afa5cefee1eb7cb3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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")
|