diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-28 18:32:37 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-28 18:32:37 +0100 |
| commit | 502bf3e151d0f9041d8f7b0ae5125906206dfe3d (patch) | |
| tree | c310959ac111c1534714eec7df1de7e500c55b42 /challenge-136/abigail/lua/ch-2.lua | |
| parent | e9489210c115b86b27ecc5e73c5440bb09c59e9a (diff) | |
| parent | dc82718186854e495ed16900b1c485d170042a18 (diff) | |
| download | perlweeklychallenge-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/ch-2.lua')
| -rw-r--r-- | challenge-136/abigail/lua/ch-2.lua | 16 |
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) |
