From f15ecab8820193c46959c93144e44496e541e946 Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Sat, 4 Jun 2016 18:14:52 +0800 Subject: [PATCH 1/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 37 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index 01d0167..4315b43 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -35,8 +35,6 @@ # return template_specializer -phi = (1+5**.5)/2 - fib_cache = {0:0, 1:1, 2:1} def Fib(n): @@ -44,11 +42,34 @@ def Fib(n): if n in fib_cache: return fib_cache[n] else: - largest = max(fib_cache) - while largest < n: - fib_cache[largest+1] = Fib(largest) + Fib(largest-1) - largest += 1 - return fib_cache[n] + result = fast_fib(n)[1] + fib_cache[n] = result + return result + +# F(2n) = (F(n-1) + F(n+1)) * F(n) +# = (F(n-1) + F(n-1) + F(n)) * F(n) +# = (2F(n-1) + F(n)) * F(n) + +# F(2n-1) = F(n-1)*F(n-1) + F(n)*F(n) + +# this returns [F(n-1), F(n)], so +# the implementation should be +# fast_fib(1000)[1] +def fast_fib(n): + global fib_cache + if n==0: return [1,0] + shift = n>>1 + if shift in fib_cache and shift-1 in fib_cache: + [a,b] = [fib_cache[shift-1],fib_cache[shift]] + else: + [a,b] = fast_fib(shift) + fib_cache[shift-1] = a + fib_cache[shift] = b + b2 = b*b + a,b = a*a+b2, (a<<1)*b+b2 + if n%2 == 1: + return [b,a+b] + return [a,b] def prod(iter): return reduce(operator.mul, iter, 1) @@ -1145,7 +1166,7 @@ def cumsum_fn(srs): 0xE7:lambda x:x.push(x.pop()*2), 0xEB:dig_fn, 0xEC:lambda x:x.toggle_preserve(), - 0xED:lambda x:x.push(phi), + 0xED:lambda x:x.push(1.618033988749895), 0xEE:lambda x:x.push(""), 0xEF:lambda x:x.push(list(set(x.pop()).intersection(x.pop()))), 0xF0:lambda x:x.push(eval(x.pop())), From 45f859915de4a146348f31d229c4bf59da33487c Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Sat, 4 Jun 2016 18:17:47 +0800 Subject: [PATCH 2/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index 4315b43..4191c37 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -68,7 +68,9 @@ def fast_fib(n): b2 = b*b a,b = a*a+b2, (a<<1)*b+b2 if n%2 == 1: + fib_cache[n-1] = b return [b,a+b] + fib_cache[n-1] = a return [a,b] def prod(iter): From 76943a800808d2f6f70a75f5b0f420334dd2449c Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Sat, 4 Jun 2016 18:32:31 +0800 Subject: [PATCH 3/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index 4191c37..6e49254 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -35,6 +35,11 @@ # return template_specializer +@memoize +def Lucas(n): + [a,b] = fast_fib(n) + return (a<<1)+b + fib_cache = {0:0, 1:1, 2:1} def Fib(n): From 6419fc7ce6e50707914c26bad19e0618a06384a7 Mon Sep 17 00:00:00 2001 From: Kenny Lau Date: Sat, 4 Jun 2016 18:33:56 +0800 Subject: [PATCH 4/4] Update SeriouslyCommands.py --- seriously/SeriouslyCommands.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/seriously/SeriouslyCommands.py b/seriously/SeriouslyCommands.py index 6e49254..3b237cb 100755 --- a/seriously/SeriouslyCommands.py +++ b/seriously/SeriouslyCommands.py @@ -35,6 +35,8 @@ # return template_specializer +phi = (1+5**.5)/2 + @memoize def Lucas(n): [a,b] = fast_fib(n) @@ -1173,7 +1175,7 @@ def cumsum_fn(srs): 0xE7:lambda x:x.push(x.pop()*2), 0xEB:dig_fn, 0xEC:lambda x:x.toggle_preserve(), - 0xED:lambda x:x.push(1.618033988749895), + 0xED:lambda x:x.push(phi), 0xEE:lambda x:x.push(""), 0xEF:lambda x:x.push(list(set(x.pop()).intersection(x.pop()))), 0xF0:lambda x:x.push(eval(x.pop())),