Module:Collatz sequence Generator
Appearance
--{{#invoke:Sandbox/R1F4T|collatz|10}}
local p = {}
function p.collatz(frame)
local n = tonumber(frame.args[1]) -- Get input from the template argument
if not n or n < 1 then
return "Please provide a positive integer."
end
local sequence = {"'''"..n.."'''"} -- Start the sequence with the initial number bolded
while n > 1 do
if n % 2 == 0 then
n = n / 2
else
n = 3 * n + 1
end
table.insert(sequence, "→ " .. n) -- Add each new number with an arrow
end
return table.concat(sequence, " ") -- Return the formatted sequence
end
return p