Created
June 10, 2013 15:17
-
-
Save nateawelch/5749590 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 n) { | |
//two is the only even prime | |
if (n == 2) { | |
return true; | |
} | |
//all less than two or divisable by two are not primes | |
if (n < 2 || n % 2 == 0) { | |
return false; | |
} | |
if(n%2==0){ | |
return false; | |
} | |
//starting from 3, and skipping evens, check up to floor(n/2) is not divisable by i | |
for (int i = 3; i < n; i+=2) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
//finally | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment