-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes_generator_sieve_of_eratosthenes.cpp
209 lines (181 loc) · 4.73 KB
/
primes_generator_sieve_of_eratosthenes.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <vector>
#include <climits>
#include <math.h>
#include <assert.h>
#include <chrono>
#include <fstream>
// make changes to vary output
bool legal_rules()
{
// TODO
// true by default
return true;
}
auto start_timer()
{
return std::chrono::steady_clock::now();
}
auto stop_timer()
{
return std::chrono::steady_clock::now();
}
auto run_time(std::chrono::_V2::steady_clock::time_point end, std::chrono::_V2::steady_clock::time_point start)
{
return std::chrono::duration<double, std::milli>(end - start).count() / 1000.0;
}
unsigned int reverse_number(unsigned int number)
{
unsigned int reversed = 0, remainder = 0;
// get total value of number
int total_value = 0;
for (int i = 10; i < number; i *= 10)
{
total_value += 1;
}
while (total_value >= 0)
{
remainder = fmod(number, 10);
reversed += remainder * pow(10, total_value);
number /= 10;
total_value--;
}
return reversed;
}
bool is_prime(unsigned int check)
{
// iterate through the integers from 2 to check,
for (int i = 2; i * i <= check; i++)
{
// test if each integer is divisible by any of the other integers in the loop.
if (check % i == 0)
{
// If it is, then it will return false;
return false;
}
}
// otherwise, it will return true.
return true;
}
std::vector<unsigned int> emirp_finder(std::vector<unsigned int> gen_primes)
{
std::vector<unsigned int> emirps;
for (auto prime_number : gen_primes)
if (is_prime(reverse_number(prime_number)))
if (prime_number > 10)
emirps.push_back(prime_number);
return emirps;
}
std::vector<unsigned int> palindromic_emirps(std::vector<unsigned int> gen_primes)
{
std::vector<unsigned int> palindrome;
for (auto prime_number : gen_primes)
{
assert(is_prime(prime_number));
if (prime_number == reverse_number(prime_number))
palindrome.push_back(prime_number);
}
return palindrome;
}
std::vector<unsigned int> sieve_of_eratosthenes(unsigned int min, unsigned int max)
{
std::vector<unsigned int> primes;
// generate positive integers
if (min < 2)
min = 2;
// MAXIMUM UNSIGNED INT
if (max > UINT_MAX || max < min)
max = UINT_MAX;
for (unsigned int gen = min; gen <= max; gen++)
// of the generated integers check for it's prime state
// if not, strike through and continue
if (is_prime(gen))
{
// if the prime state is true, push the integer into prime vector container
primes.push_back(gen);
}
return primes;
}
// driver code
int main()
{
unsigned int minimum, maximum;
std::vector<double> rtimes;
int count = 0;
std::cout << "Minimum: ";
std::cin >> minimum;
std::cout << "Maximum: ";
std::cin >> maximum;
auto start = start_timer();
std::vector<unsigned int> eratosthenes_primes = sieve_of_eratosthenes(minimum, maximum);
auto end = stop_timer();
rtimes.push_back(run_time(end, start));
start = start_timer();
std::vector<unsigned int> emirp_primes = emirp_finder(eratosthenes_primes);
end = stop_timer();
rtimes.push_back(run_time(end, start));
start = start_timer();
std::vector<unsigned int> emirp_palindrome = palindromic_emirps(emirp_primes);
end = stop_timer();
rtimes.push_back(run_time(end, start));
// check rules
assert(legal_rules());
std::cout << std::endl
<< "Generating Primes in range " << minimum << "-" << maximum << std::endl;
std::cout << "{ \n";
count = 0;
for (auto prime : eratosthenes_primes)
{
std::cout << prime << ",";
if (count % 15 == 0)
std::cout << std::endl;
count++;
}
std::cout << " \n}\n";
std::cout << "Took " << rtimes[0] << " sec" << std::endl;
std::cout << std::endl
<< "Finding Emirps in the Primes " << std::endl;
std::cout << "{ \n";
count = 0;
for (auto emirp : emirp_primes)
{
std::cout << emirp << ",";
if (count % 15 == 0)
std::cout << std::endl;
count++;
}
std::cout << " \n}\n";
std::cout << "Took " << rtimes[1] << " sec" << std::endl;
std::cout << std::endl
<< "Finding Palindromes in the Primes " << std::endl;
std::cout << "{ \n";
count = 0;
for (auto palindrome : emirp_palindrome)
{
std::cout << palindrome << ",";
if (count % 15 == 0)
std::cout << std::endl;
count++;
}
std::cout << " \n}\n";
std::cout << "Took " << rtimes[2] << " sec" << std::endl;
std::ofstream ofile("run_times.txt", std::ios::in | std::ios::app);
// writing run times into a file
ofile << std::endl
<< std::endl
<< "Using Sieve of Eratosthenes to Generate Primes" << std::endl
<< "Generating Primes in range " << minimum << "-" << maximum
<< std::endl
<< "Run time: " << rtimes[0]
<< std::endl
<< std::endl
<< "Finding Emirps in the Primes "
<< std::endl
<< "Run time: " << rtimes[1]
<< std::endl
<< std::endl
<< "Finding Palindromes in the Primes "
<< std::endl
<< "Run time: " << rtimes[1] << std::endl;
return 0;
}