local p =
function p.collatz(frame) local n = tonumber(frame.args[1]) -- Input number if not n or n < 1 then return "Please provide a positive integer." end
local bold = frame.args['bold'] -- Bold parameter (even/odd) local max = tonumber(frame.args['max']) -- Maximum steps local display_steps = frame.args['steps']
"yes" -- Hide the sequence local counter = frame.args['counter'] -- Counter parameter (even, odd, or both)
-- Check if both 'hide' and 'max' are set (conflict) if hide_sequence and max then return "Error: Sequence is hidden. Please remove the 'max' parameter." end
local sequence, count =, 0 -- Initialize sequence and step counter local even_count, odd_count = 0, 0 -- Counters for even and odd numbers
-- Helper to conditionally bold numbers local function maybe_bold(num) local result = tostring(num) if (num % 2
"even") or (num % 2 ~= 0 and bold
-- Function to handle the Collatz logic and format sequence local function collatz_step(num) if num % 2
0 and num / 2 or 3 * num + 1 -- Next Collatz step end
-- Main Collatz loop while n > 1 do if max and count >= max then -- Stop if max steps reached table.insert(sequence, "..." .. tostring(math.floor(math.log(n, 2)) + 1) .. " more") -- Estimate remaining steps break end n = collatz_step(n) -- Perform Collatz step end collatz_step(n) -- Final step to 1
-- Handle 'hide' option if hide_sequence then local result = "" -- Initialize result for hidden sequence
-- Add counter information if counter
"odd" then result = "(Odd numbers: " .. odd_count .. ")" elseif counter
-- Add step count if requested if display_steps then result = result .. (result ~= "" and " " or "") .. "(Total steps: " .. (count - 1) .. ")" -- Add step info end
-- If neither counters nor steps were added, return default hidden message if result
-- Build the result: sequence and optionally steps local result = table.concat(sequence, ", ") if display_steps then result = result .. " (Total steps: " .. (count - 1) .. ")" -- Steps should now decrement by 1 end
-- Add even and odd counter information if the 'counter' parameter is set if counter
"odd" then result = result .. odd_count .. "odd" elseif counter
return resultend
return p