summaryrefslogtreecommitdiffstats
path: root/14.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 /14.lua
downloadprojecteuler-7e7b56db42ceb8d2b8973eae678fa4b58d5d3659.tar.gz
projecteuler-7e7b56db42ceb8d2b8973eae678fa4b58d5d3659.zip
add old solutions
Diffstat (limited to '14.lua')
-rw-r--r--14.lua20
1 files changed, 20 insertions, 0 deletions
diff --git a/14.lua b/14.lua
new file mode 100644
index 0000000..96498ec
--- /dev/null
+++ b/14.lua
@@ -0,0 +1,20 @@
+function collatz(n)
+ local sum = 1
+ while true do
+ if n == 1 then break end
+ if n % 2 == 0 then n = n / 2
+ else n = n * 3 + 1 end
+ sum = sum + 1
+ end
+ return sum
+end
+
+local max = 0
+local maxnum = 0
+for i = 1, 999999 do
+ local sum = collatz(i)
+ print(i .. ": " .. sum)
+ if sum > max then max = sum; maxnum = i end
+end
+print("--------")
+print(maxnum .. ": " .. max)