Created
March 5, 2012 15:34
-
-
Save gideondsouza/1978856 to your computer and use it in GitHub Desktop.
normal (brute force) method to check for primes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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