aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-136/abigail/lua/ch-2.lua')
-rw-r--r--challenge-136/abigail/lua/ch-2.lua16
1 files changed, 12 insertions, 4 deletions
diff --git a/challenge-136/abigail/lua/ch-2.lua b/challenge-136/abigail/lua/ch-2.lua
index 06d92984f4..e955e8cc81 100644
--- a/challenge-136/abigail/lua/ch-2.lua
+++ b/challenge-136/abigail/lua/ch-2.lua
@@ -8,11 +8,19 @@
-- Run as: lua ch-2.lua < input-file
--
+local cache = {}
+
function _count (target, this_fib, prev_fib)
- if target < this_fib then return 0 end
- if target == this_fib then return 1 end
- return (_count (target - this_fib, this_fib + prev_fib, this_fib) +
- _count (target, this_fib + prev_fib, this_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)