-- 5.宣告一個function,輸入一個正整數,顯示所有小於等於該正整數的質數,若輸入的數非正整數要報錯
-- ex:
-- input : 5
-- return : 1,2,3,5
-- input : 10
-- return : 1,2,3,5,7
-- input : "a"
-- return : "Error, input must be a positive integer"
function Prime_number(x)
s = true
for i = 2, x-1, 1 do
if(x%i==0)
then
s = false
end
end
return s
end
-- Prime_number(11)
function EX_5(n)
if(type(n) == "number" and n > 0 and n%1==0)
then
local P = {1}
for j = 2, n, 1 do
if(Prime_number(j))
then
table.insert(P, j)
end
end
print(table.concat(P,","))
else
print("Error, input value must be a positive integer.")
end
return P
end
EX_5(123)
EX_5(asd)
To embed this program on your website, copy the following code and paste it into your website's HTML: