-
Notifications
You must be signed in to change notification settings - Fork 3
/
primes.cpp
95 lines (82 loc) · 2.45 KB
/
primes.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <vector>
#include <algorithm>
#include <iostream>
#include <cassert>
typedef std::vector<char> Primes;
Primes sieve(unsigned long size) {
Primes res(size, 1);
res[0] = 0;
res[1] = 0;
for (unsigned long i = 2; i < res.size(); ++i) {
if (res[i] == 0) continue; // already not a prime
unsigned long j = i + i;
while (j < res.size()) {
res[j] = 0;
j += i;
}
}
return res;
}
// count all the primes in a sequence (n, n+step, n+2*step,...) for count values
void findEm(const Primes &primes, unsigned long max,
unsigned long count, unsigned long step) {
assert(primes.size() > max + (count * step));
for (unsigned long i = 2; i < max; ++i) {
if (primes[i]) {
int cnt = 1;
unsigned long val = i;
for (int j = 0; j < count; ++j) {
val += step;
if (primes[val])
++cnt;
}
if (cnt > 16)
std::cout << i << " (" << step << "): " << cnt << std::endl;
}
}
}
// find all the consecutive primes in a sequence (n, n+step, n+2*step,...)
void findConsecutive(const Primes &primes, unsigned long max, unsigned long step) {
for (unsigned long i = 2; i < max; ++i) {
int cnt = 0;
unsigned long val = i;
while (val < primes.size() && primes[val]) {
++cnt;
val += step;
}
if (cnt > 10) {
if (val >= primes.size())
std::cout << "Consecutive: " << i << " (" << step << "): " << cnt << " (and maybe more)" << std::endl;
else
std::cout << "Consecutive: " << i << " (" << step << "): " << cnt << std::endl;
}
}
}
void findOne(const Primes &primes, unsigned long value,
unsigned long count, unsigned long step) {
assert(primes.size() > value + (count * step));
if (primes[value]) {
int cnt = 1;
unsigned long val = value;
// we go up to 'count - 1' because we've already counted one
for (int j = 0; j < count - 1; ++j) {
val += step;
if (primes[val])
++cnt;
std::cout << val << (primes[val] ? " (prime)" : " (not prime)") << std::endl;
}
if (cnt > 8)
std::cout << value << " (" << step << "): " << cnt << std::endl;
}
}
int main (int, char **) {
const unsigned long N = 10000000;
const unsigned long STEP = 10000;
const unsigned long COUNT = 20;
Primes primes = sieve(N + (COUNT * STEP) + 1);
// findOne(primes, 5UL, 10UL, 6UL);
// for (unsigned long step = 2; step <= STEP; step += 2)
// findEm(primes, N, COUNT, step);
for (unsigned long step = 2; step <= STEP; step += 2)
findConsecutive(primes, N, step);
}