primes/primes.4.lua

35 lines
496 B
Lua
Raw Permalink Normal View History

2011-03-08 08:01:27 +01:00
-- Lua 4 version of a Prime Number Finder
-- haplo@mindstab.net
function primes(max)
cnt = 3
while cnt <= max do
sq = sqrt(cnt)
isp = 1
i = 3
while i <= sq and isp ~= 0 do
isp = mod(cnt, i)
i = i+2
end
if isp ~= 0 then
print(cnt)
end
cnt = cnt + 2
end
end
--argc = table.getn(arg)
argc = read()
max = tonumber(argc)
if max < 1 then
print("Usage: echo [Max Num] | lua primes.lua")
else
if not max then
print("Invalid Max Num")
else
primes(max)
end
end