aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/lua/ch-2.lua
blob: e955e8cc81a07b6d8ca2a57fa98ab33d914f04b9 (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
#!/opt/local/bin/lua

--
-- See ../README.md
--

--
-- Run as: lua ch-2.lua < input-file
--

local cache = {}

function _count (target, this_fib, prev_fib)
    local key = target .. ";" .. this_fib
    if cache [key] == nil then
            if target <  this_fib then cache [key] = 0
        elseif target == this_fib then cache [key] = 1
        else                           cache [key] =
              _count (target - this_fib, this_fib + prev_fib, this_fib) +
              _count (target,            this_fib + prev_fib, this_fib)
        end
    end
    return cache [key]
end

function count (target)
    return (_count (target, 1, 1))
end

for line in io . lines () do
    print (count (tonumber (line)))
end