master amadaluzia/rosetta-collatz / lua / rosetta-collatz.lua
 1local function collatz(n)
 2  if n % 2 == 0 then
 3    return math.floor(n / 2)
 4  else
 5    return 3 * n + 1
 6  end
 7end
 8
 9local function collatzSequence(n)
10  local buf = string.format("%d: ", n)
11  local i = n
12  while true do
13    if i == 1 then
14      buf = string.format("%s1", buf, i)
15      break
16    end
17    buf = string.format("%s%d, ", buf, i)
18    i = collatz(i)
19  end
20  return buf
21end
22
23for i = 1,10000 do
24  print(collatzSequence(i))
25end
26