Last active
December 21, 2016 22:14
-
-
Save WildGenie/69f0cea75b2fee48d292345e5a155d40 to your computer and use it in GitHub Desktop.
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(int input) | |
{ | |
//2 and 3 are primes | |
if (input == 2 || input == 3) | |
return true; | |
else if (input % 2 == 0 || input % 3 == 0) | |
return false; //Is divisible by 2 or 3? | |
else | |
{ | |
for (int i = 5; i * i <= input; i += 6) | |
{ | |
if (input % i == 0 || input % (i + 2) == 0) | |
return false; | |
} | |
return true; | |
} | |
} | |
bool IsPrime2(int candidate) | |
{ | |
if ( candidate % 2 <= 0 ) | |
{ | |
return candidate == 2; | |
} | |
int power2 = 9; | |
for ( int divisor = 3; power2 <= candidate; divisor += 2 ) | |
{ | |
if ( candidate % divisor == 0 ) | |
return false; | |
power2 += divisor * 4 + 4; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment