summaryrefslogtreecommitdiffstats
path: root/12.lua
diff options
context:
space:
mode:
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