Created
September 12, 2014 03:39
-
-
Save 1995eaton/ede0dc0573302eb9ddb6 to your computer and use it in GitHub Desktop.
Is prime
This file contains 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
#include <iostream> | |
#include <cmath> | |
template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> | |
bool is_prime(T n) { | |
if (n < 2) { | |
return false; | |
} | |
for (T i = 2, sq = sqrt(n) + 1; i != sq; i++) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() { | |
std::cout << is_prime(101) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment