aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/abigail/lua
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-10-28 18:32:37 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-10-28 18:32:37 +0100
commit502bf3e151d0f9041d8f7b0ae5125906206dfe3d (patch)
treec310959ac111c1534714eec7df1de7e500c55b42 /challenge-136/abigail/lua
parente9489210c115b86b27ecc5e73c5440bb09c59e9a (diff)
parentdc82718186854e495ed16900b1c485d170042a18 (diff)
downloadperlweeklychallenge-club-502bf3e151d0f9041d8f7b0ae5125906206dfe3d.tar.gz
perlweeklychallenge-club-502bf3e151d0f9041d8f7b0ae5125906206dfe3d.tar.bz2
perlweeklychallenge-club-502bf3e151d0f9041d8f7b0ae5125906206dfe3d.zip
Merge branch 'master' into devel
Diffstat (limited to 'challenge-136/abigail/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)