Well, I don't know that language, but it looks to be fairly easy to read. Your first problem is this line:
if (primenum / primenum) = 1 or primenum / 1 = 0 then
This is testing to see if primenum
divided by primenum
is one or if primenum
divided by one is zero. The first condition will always be true and thus your algorithm will report that every integer is a prime number.
Let's recall the definition of a prime number. A natural number n
is prime if it has exactly two distinct natural number divisors. This means that to check and see if n
is prime you must validate that there are no other divisors of n
except for 1
and n
itself (note that implicitly n
can not be equal to 1
otherwise its only divisors are 1
and 1
which are not distinct). To do this, just consider all possible numbers that could be divisors of n
excluding 1
and n
itself and check if any of them divide n
. This means that we just loop from 2
to n - 1
checking if any of these numbers evenly divide n
. The natural numbers in the range 2
to n - 1
are the only possible numbers that could invalidate n
from being prime.
Thus, the most naive way to implement a test as to whether a number is prime is following. Accept as input a number n
. Then, check to see if n
is less than 2
. If it is it can not be a prime number. Then, loop from 2
to n - 1
; call the loop variable k
. Check to see if any k
in 2
to n - 1
evenly divides n
(if n mod k = 0
). If there is such a k
, then n
can not be prime and you can break from the loop. Otherwise, if the loop terminates without breaking then n
is prime. So, in pseudocode
integer n
get n
boolean flag
if n < 2
flag = false
else
flag = true
for k = 2 to n - 1
if n mod k = 0
flag = false
break
if flag
print "prime"
else
print "not prime"
Now, just one minor comment about your code. Don't name the input primenum
. A reader of your code might that think that primenum
is in fact a prime number because you named it so. A name like valueToTest
would be strongly preferred here.