Yang has a good idea. Let me elaborate.
A number is prime if it is not evenly divisible by any number other than itself and 1.
So, if you check every number from 2 to N, and none of them divide into N, then N is prime.
You can further optimize this by only checking from 2 to the square root of N. Now, I don't exactly understand why this is true, but it's based on very sound mathematical reasoning, so let's just accept it.
1 2 3 4 5 6
boolean is_it_prime(int N){
for(int i = 2; i <= sqrt(N); i++){
if ((N % i) == 0){returnfalse;} //divides evenly- NOT PRIME!
}
returntrue; //no numbers divided evenly- PRIME!
}
Depending upon your application, perhaps you can use common divisibility rules to exclude a number as prime.
Even numbers are not prime.
If last digit is 5, not prime (divisible by 5).
If sum of digits is divisible by 3, number is divisible by 3 and not prime.