Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created September 12, 2014 03:39
Show Gist options
  • Save 1995eaton/ede0dc0573302eb9ddb6 to your computer and use it in GitHub Desktop.
Save 1995eaton/ede0dc0573302eb9ddb6 to your computer and use it in GitHub Desktop.
Is prime
#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