RosettaCodeData/Task/Harshad-or-Niven-series/Lua/harshad-or-niven-series.lua
2015-11-18 06:14:39 +00:00

28 lines
417 B
Lua

function isHarshad(n)
local s=0
local n_str=tostring(n)
for i=1,#n_str do
s=s+tonumber(n_str:sub(i,i))
end
return n%s==0
end
local count=0
local harshads={}
local n=1
while count<20 do
if isHarshad(n) then
count=count+1
table.insert(harshads, n)
end
n=n+1
end
print(table.concat(harshads, " "))
local h=1001
while not isHarshad(h) do
h=h+1
end
print(h)