Skip to content

Instantly share code, notes, and snippets.

@lotz84
Created February 11, 2015 14:41
Show Gist options
  • Save lotz84/61c058b588c13a63bca8 to your computer and use it in GitHub Desktop.
Save lotz84/61c058b588c13a63bca8 to your computer and use it in GitHub Desktop.
#include <iostream>
template <int N, int M>
struct is_factor {
static const bool value = N % M == 0;
};
template <int N, int M>
struct divide_test {
static const bool value = !is_factor<N, M>::value && divide_test<N, M-1>::value;
};
template <int N>
struct divide_test<N, 1> {
static const bool value = true;
};
template <int N>
struct is_prime {
static const bool value = divide_test<N, N-1>::value;
};
int main() {
if (is_prime<23>::value) {
std::cout << "Prime!" << std::endl;
} else {
std::cout << "Not prime!" << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment