-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprimes.cpp
More file actions
53 lines (45 loc) · 812 Bytes
/
primes.cpp
File metadata and controls
53 lines (45 loc) · 812 Bytes
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
//
// Erwin Unruh implemented this first, prime numbers were printed out as warnings
//
#include <iostream>
template <int p, int i>
class is_prime {
public:
enum { prim = (p==2) || ((p%i) && is_prime<( i>2 ? p : 0),i-1>::prim) };
};
template<>
class is_prime<0,0> {
public:
enum {prim=1};
};
template<>
class is_prime<0,1> {
public:
enum {prim=1};
};
template <int i>
class Prime_print { // primary template for loop to print prime numbers
public:
Prime_print<i-1> a;
enum { prim = is_prime<i,i-1>::prim
};
void f() {
if (prim)
{
std::cout << "prime number:" << i << std::endl;
}
a.f();
}
};
template<>
class Prime_print<1> { // full specialization to end the loop
public:
enum {prim=0};
void f() {
};
};
int main()
{
Prime_print<100> a;
a.f();
}