RosettaCodeData/Task/Casting-out-nines/Pluto/casting-out-nines.pluto
2025-08-11 18:05:26 -07:00

30 lines
748 B
Text

local function cast_out(base, start, finish)
local b = base - 1
local ran = range(0, b - 1):filter(|n| -> n % b == (n * n) % b)
local x = start // b
local result = {}
while true do
for ran as n do
local k = b * x + n
if k >= start then
if k > finish then return result end
result:insert(k)
end
end
++x
end
end
local function print_array(a, w = 3, n = 10)
for i = 1, #a do
io.write(string.format($"%{w}d ", a[i]))
if i % n == 0 then print() end
end
if #a % 10 != n then print() end
end
print_array(cast_out(16, 1, 255))
print()
print_array(cast_out(10, 1, 99))
print()
print_array(cast_out(17, 1, 288))