-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
49 lines (40 loc) · 991 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace detail {
template <int i> struct IsPrime
{
private:
template <int k, bool cond = (k*k <= i)> struct Helper
{
static const bool result = (i % k)? Helper<k + 1>::result : false;
};
template <int k> struct Helper<k, false>
{
static const bool result = true;
};
public:
static const bool result = Helper<2>::result;
};
template <int i, bool cond = IsPrime<i + 1>::result> struct Next
{
static const int result = Next<i + 1>::result;
};
template <int i> struct Next<i, true>
{
static const int result = i + 1;
};
}
// Calculate ith prime number
template <int i> struct Prime
{
static const int result = detail::Next<Prime<i - 1>::result>::result;
};
template <> struct Prime<0>
{
static const int result = 2;
};
int main()
{
static_assert(Prime<2>::result == 5, "Good!");
static_assert(Prime<10>::result == 31, "Good!");
static_assert(Prime<50>::result == 234, "Wrong! Prime[50] = 233!");
return 0;
}