Created
February 11, 2015 14:41
-
-
Save lotz84/61c058b588c13a63bca8 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
#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