summaryrefslogtreecommitdiffstats
path: root/002.lua
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-05-14 19:51:41 -0500
committerJesse Luehrs <doy@tozt.net>2009-05-14 19:51:41 -0500
commitc3ff98ee4aa79bcaa07c8a478e96539c2a512e73 (patch)
tree0b1a04ee7e5b05ec23dcfa274d4258f8acbcdf9d /002.lua
parent394121b98178246a0b1063e9104f8878cf2a17e5 (diff)
downloadprojecteuler-c3ff98ee4aa79bcaa07c8a478e96539c2a512e73.tar.gz
projecteuler-c3ff98ee4aa79bcaa07c8a478e96539c2a512e73.zip
rename files for better sorting
Diffstat (limited to '002.lua')
-rw-r--r--002.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/002.lua b/002.lua
new file mode 100644
index 0000000..d70cdc3
--- /dev/null
+++ b/002.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)