-
Notifications
You must be signed in to change notification settings - Fork 0
/
primechecker.cc
86 lines (75 loc) · 1.78 KB
/
primechecker.cc
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
// a eratosthenes sieve method of generating primes. It takes four seconds
// to check the first ten million ints for primeness, and less than a second for the
// first two million.
#include <bitset>
#include <iostream>
#include "primechecker.h"
using namespace std;
PrimeChecker::PrimeChecker() {
// 0 and 1 are not prime
a.set(0, 1);
a.set(1, 1);
prime_index = 0;
long primer = 2;
while (primer < (max / 2)) {
for (long i = 2; (primer * i) < max; i++) {
long product = primer * i;
// 1 means not a prime
a.set(product, 1);
}
primer++;
}
cout << "Finished generating primes." << endl;
}
bool PrimeChecker::IsPrime(long i) {
if (i > max) {
cout << "Out of bounds " << i << endl;
return false;
}
// 1 means not a prime
if (a[i] == 1)
return false;
return true;
}
void PrimeChecker::CountPrimes() {
cout << "Number of primes below " << max << " is "
<< (max - a.count()) << endl;
}
long PrimeChecker::SumPrimes() {
long sum = 0;
for (int i = 0; i < max; i++) {
if (a[i] == 0) {
sum += i;
}
}
cout << "The sum of primes below " << max << " is: " << sum << endl;
return sum;
}
long PrimeChecker::GetNthPrime(int n) {
int count = 0;
for (int i = 0; i < max; i++) {
if (a[i] == 0) {
count++;
}
if (count == n) {
cout << "The " << n << "th prime is " << i << endl;
return i;
}
}
return -1;
}
long PrimeChecker::GetNextPrime() {
for (int i = prime_index + 1; i < max; i++) {
if (a[i] == 0) {
prime_index = i;
return i;
}
}
// hit the max without finding a prime
cout << "Not checking primes bigger than " << max << ": nothing to"
<< " return. Returning -1." << endl;
return -1;
}
void PrimeChecker::ResetIndex() {
prime_index = 0;
}