Skip to content

Instantly share code, notes, and snippets.

@gideondsouza
Created March 5, 2012 15:34
Show Gist options
  • Save gideondsouza/1978856 to your computer and use it in GitHub Desktop.
Save gideondsouza/1978856 to your computer and use it in GitHub Desktop.
normal (brute force) method to check for primes
bool IsPrime(long n)
{
int c = 0;
for (long i = 1; i < n; i++)
{//loop up-till the number
if (n % i == 0)//check if its divisible by i
{
c++;
if (c > 1) { return false; }//short-circuit..
} //^^if it has more than one divisor it can't be prime
}
if (c == 1)
{
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment