-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP124.CompileTimePrime.cpp
More file actions
72 lines (63 loc) · 1.74 KB
/
P124.CompileTimePrime.cpp
File metadata and controls
72 lines (63 loc) · 1.74 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
template<unsigned p, unsigned d>
struct DoIsPrime
{
static constexpr bool value = (p%d != 0) && DoIsPrime<p, d-1>::value;
};
// partial sepcialization, the end point of recursion
template<unsigned p>
struct DoIsPrime<p, 2>
{
static constexpr bool value = (p%2 != 0);
};
template<unsigned p>
struct IsPrime
{
// start recursion with divisor from p/2
static constexpr bool value = DoIsPrime<p, p/2>::value;
};
// sepcial cases
template<>
struct IsPrime<0> { static constexpr bool value = false; };
template<>
struct IsPrime<1> { static constexpr bool value = false; };
template<>
struct IsPrime<2> { static constexpr bool value = true; };
template<>
struct IsPrime<3> { static constexpr bool value = true; };
// since C++11, use constexpr
// only one return statement can be in constexpr function
constexpr bool doIsPrime11(unsigned p, unsigned d)
{
return d != 2 ? (p % d != 0) && doIsPrime11(p, d-1)
: (p % 2 != 0);
}
constexpr bool isPrime11(unsigned p)
{
return p < 4 ? !(p < 2) : doIsPrime11(p, p/2);
}
// since C++14
constexpr bool isPrime14(unsigned p)
{
for (unsigned d = 2; d <= p/2; ++d)
{
if (p % d == 0)
{
return false;
}
}
return p > 1;
}
int main(int argc, char const *argv[])
{
std::cout << std::boolalpha << IsPrime<97>::value << std::endl;
std::cout << IsPrime<107>::value << std::endl;
std::cout << IsPrime<781>::value << std::endl; // false
std::cout << isPrime11(97) << std::endl;
std::cout << isPrime11(107) << std::endl;
std::cout << isPrime11(781) << std::endl;
std::cout << isPrime14(97) << std::endl;
std::cout << isPrime14(107) << std::endl;
std::cout << isPrime14(781) << std::endl;
return 0;
}