summaryrefslogtreecommitdiffstats
path: root/12.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 /12.lua
downloadprojecteuler-7e7b56db42ceb8d2b8973eae678fa4b58d5d3659.tar.gz
projecteuler-7e7b56db42ceb8d2b8973eae678fa4b58d5d3659.zip
add old solutions
Diffstat (limited to '12.lua')
-rw-r--r--12.lua31
1 files changed, 31 insertions, 0 deletions
diff --git a/12.lua b/12.lua
new file mode 100644
index 0000000..7ef1c0d
--- /dev/null
+++ b/12.lua
@@ -0,0 +1,31 @@
+-- only valid when n > 1
+function num_factors(n)
+ local ret = 2
+ local test = 2
+ local limit = math.sqrt(n)
+ while test < limit do
+ if n % test == 0 then
+ ret = ret + 2
+ end
+ test = test + 1
+ end
+ if limit == math.floor(limit) then ret = ret + 1 end
+ return ret
+end
+
+generate_triangle = coroutine.wrap(function()
+ local num = 0
+ local add = 1
+ while true do
+ num = num + add
+ add = add + 1
+ coroutine.yield(num)
+ end
+end)
+
+while true do
+ local n = generate_triangle()
+ local nn = num_factors(n)
+ print(n .. ": " .. nn)
+ if nn > 500 then break end
+end