-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprime_numbers.cpp
39 lines (33 loc) · 1.17 KB
/
prime_numbers.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
#include <iostream>
#include <vector>
int main() {
// Read the upper limit for the range from user input
int p;
std::cout << "range: ";
std::cin >> p;
// Initialize a vector of boolean values representing primality, initially all set to true
std::vector<bool> primes(p, true);
// Iterate through each number in the range starting from 2 to p-1
for (int possiblePrime = 2; possiblePrime < p; ++possiblePrime) {
// If the number is still marked as prime
if (primes[possiblePrime]) {
// Mark all multiples of the number as not prime (false)
for (int multiple = possiblePrime * 2; multiple < p; multiple += possiblePrime) {
primes[multiple] = false;
}
}
}
// Create a vector of prime numbers by filtering indices marked as true and greater than 1
std::vector<int> primeNumbers;
for (int index = 2; index < p; ++index) {
if (primes[index]) {
primeNumbers.push_back(index);
}
}
// Print the list of prime numbers
for (int prime : primeNumbers) {
std::cout << prime << " ";
}
std::cout << std::endl;
return 0;
}