RosettaCodeData/Task/Disarium-numbers/Pluto/disarium-numbers.pluto
2025-08-11 18:05:26 -07:00

29 lines
824 B
Text

do -- find some Disarium numbers, numbers that are equal to their digit position power sums
-- compute the nth powers of 0-9 #
local power <const> = { { [0] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, {}, {}, {}, {}, {}, {}, {}, {}, {} }
for n = 2, 9 do
power[n][0] = 0
for d = 1, 9 do
power[n][d] = power[n - 1][d] * d
end
end
local function isDisarium (x)
local str, sum = tostring(x), 0
for pos = 1, #str do
local digit <const> = tonumber(str[pos])
sum += power[pos][digit]
end
return sum == x
end
local count, n = 0, 0
while count < 19 do
if isDisarium( n ) then
++ count
io.write( " ", n )
end
++ n
end
end