This is a quick and easy prime number test because I'm seriously sick of searching all over the web for a faster way. So I wrote three and decided that the one with the least lines of code actually looks like it works better.
bool isPrime(long n)
{
if (n < 2) return false;
if (n < 4) return true;
if (n % 2 == 0) return false; // if number modulo 2 returns 0 then it's even, and not prime
const unsigned int i = sqrt((double)n) + 1;
for (unsigned int j = 3; j <= i; j += 2)
{
if (n % j == 0)
return false;
}
return true;
}
Oh and you must #include <math.h>
For more information look up Sieve of Eratosthenes and AKS. The codes based on both.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock