primes/primes.5.lua

33 lines
481 B
Lua

-- Lua 5 version of a Prime Number Finder
-- haplo@mindstab.net
function primes(max)
cnt = 3
while cnt <= max do
sq = math.sqrt(cnt)
isp = 1
i = 3
while i <= sq and isp ~= 0 do
isp = math.mod(cnt, i)
i = i+2
end
if isp ~= 0 then
print(cnt)
end
cnt = cnt + 2
end
end
argc = table.getn(arg)
if argc < 1 then
print("Usage: primeslua [Max Num]")
else
max = tonumber(arg[1])
if not max then
print("Invalid Max Num")
else
primes(max)
end
end