Disarium numbers: Difference between revisions

Added Lua version
No edit summary
(Added Lua version)
Line 1,831:
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>
 
=={{header|Lua}}==
Like most other solutions, this stops at 19. Computation time aside, the 20th Disarium number is greater than 2^53, above which double-precision floating-point format (which is Lua's in-built number type) has insufficient precision to distinguish between one integer and the next.
<syntaxhighlight lang="lua">function isDisarium (x)
local str, sum, digit = tostring(x), 0
for pos = 1, #str do
digit = tonumber(str:sub(pos, pos))
sum = sum + (digit ^ pos)
end
return sum == x
end
 
local count, n = 0, 0
while count < 19 do
if isDisarium(n) then
count = count + 1
io.write(n .. " ")
end
n = n + 1
end</syntaxhighlight>
{{out}}
<pre>0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</pre>
 
=={{header|MAD}}==
31

edits