summaryrefslogtreecommitdiffstats
path: root/2.lua
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-05-13 23:32:40 -0500
committerJesse Luehrs <doy@tozt.net>2009-05-13 23:32:40 -0500
commit7e7b56db42ceb8d2b8973eae678fa4b58d5d3659 (patch)
tree7a5cb816809d632e30227c1e39485ed63b500e80 /2.lua
downloadprojecteuler-7e7b56db42ceb8d2b8973eae678fa4b58d5d3659.tar.gz
projecteuler-7e7b56db42ceb8d2b8973eae678fa4b58d5d3659.zip
add old solutions
Diffstat (limited to '2.lua')
-rw-r--r--2.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/2.lua b/2.lua
new file mode 100644
index 0000000..d70cdc3
--- /dev/null
+++ b/2.lua
@@ -0,0 +1,30 @@
+local function memoize(fn)
+ local t = {}
+ return function(x)
+ local y = t[x]
+ if y == nil then y = fn(x); t[x] = y end
+ return y
+ end
+end
+
+fib = memoize(function(n)
+ if n < 3 then return 1 end
+ return fib(n - 1) + fib(n - 2)
+end)
+
+local root5 = math.sqrt(5)
+local phi = (1 + root5)/2
+local function fib2(n)
+ return math.floor((phi^n-(1-phi)^n)/root5+0.5)
+end
+
+local sum = 0
+local i = 0
+while true do
+ i = i + 1
+ local n = fib(i)
+ if n > 1000000 then break end
+ if n % 2 == 0 then sum = sum + n end
+end
+
+print(sum)