-
Notifications
You must be signed in to change notification settings - Fork 0
/
primec++.cpp
115 lines (109 loc) · 3.73 KB
/
primec++.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
#include <iostream>
#include <vector>
#include <cmath>
#include <stdexcept>
/**
* Returns a vector of prime numbers up to n using the Sieve of Eratosthenes algorithm.
*/
std::vector<int> sieve_of_eratosthenes(int n) {
if (n < 2) {
return {};
}
std::vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false; // 0 and 1 are not primes
for (int p = 2; p <= std::sqrt(n); ++p) {
if (is_prime[p]) {
for (int multiple = p * p; multiple <= n; multiple += p) {
is_prime[multiple] = false;
}
}
}
std::vector<int> primes;
for (int p = 2; p <= n; ++p) {
if (is_prime[p]) {
primes.push_back(p);
}
}
return primes;
}
/**
* Finds all prime numbers in the range [start, end].
*/
std::vector<int> find_primes_in_range(int start, int end) {
if (start > end || start < 0 || end < 0) {
return {};
}
std::vector<int> primes_up_to_end = sieve_of_eratosthenes(end);
std::vector<int> primes_in_range;
for (int p : primes_up_to_end) {
if (p >= start) {
primes_in_range.push_back(p);
}
}
return primes_in_range;
}
/**
* Provides the last three prime numbers and the count of prime numbers in the range [start, end].
*/
std::pair<std::vector<int>, int> prime_statistics(int start, int end) {
std::vector<int> primes_in_range = find_primes_in_range(start, end);
int count_of_primes = primes_in_range.size();
std::vector<int> last_three_primes;
if (primes_in_range.size() >= 3) {
last_three_primes = {primes_in_range.end() - 3, primes_in_range.end()};
} else {
last_three_primes = primes_in_range;
}
return {last_three_primes, count_of_primes};
}
/**
* Prompts the user to enter the start and end of the range, with validation.
*/
std::pair<int, int> get_user_input() {
int start, end;
while (true) {
try {
std::cout << "Please enter the start of the range (non-negative integer): ";
std::cin >> start;
std::cout << "Please enter the end of the range (non-negative integer): ";
std::cin >> end;
if (start > end) {
std::cout << "Error: Start of the range must be less than or equal to the end of the range.\n";
} else if (start < 0 || end < 0) {
std::cout << "Error: Range values must be non-negative.\n";
} else {
return {start, end};
}
} catch (const std::exception& e) {
std::cout << "Error: Invalid input. Please enter integer values.\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
}
}
}
/**
* Displays the results of the prime number calculations.
*/
void display_results(int start, int end, const std::vector<int>& last_three_primes, int count_of_primes) {
if (!last_three_primes.empty()) {
std::cout << "The last prime numbers in the range [" << start << ", " << end << "] are: ";
for (size_t i = 0; i < last_three_primes.size(); ++i) {
std::cout << last_three_primes[i];
if (i < last_three_primes.size() - 1) {
std::cout << ", ";
}
}
std::cout << "\n";
} else {
std::cout << "No prime numbers found in the range [" << start << ", " << end << "]\n";
}
std::cout << "The number of prime numbers in this range is: " << count_of_primes << "\n";
}
int main() {
// Get user input for the range
auto [start, end] = get_user_input();
// Calculate and display the results
auto [last_three_primes, count_of_primes] = prime_statistics(start, end);
display_results(start, end, last_three_primes, count_of_primes);
return 0;
}