#include <iostream>
#include <cmath>
using namespace std;
const int N = 1000;
void sieve(bool prime[]) {
for (int i = 2; i <= N; i++) {
if (prime[i] == true) {
for (int j = i * 2; j <= N; j += i)
prime[j] = false;
}
int main() {
bool prime[N + 1];
for (int i = 0; i <= N; i++)
prime[i] = true;
prime[0] = prime[1] = false;
sieve(prime);
cout << "Прості числа в діапазоні [1..1000]:" << endl;
for (int i = 2; i <= N; i++)
if (prime[i]) cout << i << " ";
cout << endl;
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
#include <cmath>
using namespace std;
const int N = 1000;
void sieve(bool prime[]) {
for (int i = 2; i <= N; i++) {
if (prime[i] == true) {
for (int j = i * 2; j <= N; j += i)
prime[j] = false;
}
}
}
int main() {
bool prime[N + 1];
for (int i = 0; i <= N; i++)
prime[i] = true;
prime[0] = prime[1] = false;
sieve(prime);
cout << "Прості числа в діапазоні [1..1000]:" << endl;
for (int i = 2; i <= N; i++)
if (prime[i]) cout << i << " ";
cout << endl;
return 0;
}