Narcissistic decimal number: Difference between revisions

Added Lua version
(GP)
(Added Lua version)
Line 1,242:
25 9926315
</pre>
=={{header|Lua}}==
This is a simple/naive/slow method but it still splits out the requisite 25 in less than a minute using LuaJIT on a 2.5GHz machine.
<lang Lua>function isNarc (n)
local m, sum, digit = string.len(n), 0
for pos = 1, m do
digit = tonumber(string.sub(n, pos, pos))
sum = sum + digit^m
end
return sum == n
end
 
local n, count = 0, 0
repeat
if isNarc(n) then
io.write(n .. " ")
count = count + 1
end
n = n + 1
until count == 25</lang>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315
</pre>
=={{header|Mathematica}}==
<lang Mathematica>narc[1] = 0;
Anonymous user