Skip to content

Instantly share code, notes, and snippets.

@nateawelch
Created June 10, 2013 15:17
Show Gist options
  • Save nateawelch/5749590 to your computer and use it in GitHub Desktop.
Save nateawelch/5749590 to your computer and use it in GitHub Desktop.
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