prime
to test if a number is prime. The function will return true
if the
number if prime, and false
if the number is not prime. To keep things simple, we'll implement something related to the
"naive" test described on Wikipedia (link).
if
statement and the for
statement.
function prime(n)
if ??? == 0 then
return(false)
end
for i=??,??,?? do
if n % i == 0 then
return(false)
end
end
return(true)
end
print("Number to test?")
n=input()
if prime(n) then
print("yes,",n,"is prime.")
else
print(n,"is not prime.")
end
prime
function, you need to fix the following:
if
statement to see if 2 divides evenly into n, causing the
function to return false
?
for
statement, to start at 3 and count up to √n, going next
to 5, then 7, then 9, etc. (i.e. only odd numbers)?