-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsieve.cpp
45 lines (39 loc) · 823 Bytes
/
sieve.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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
// Sieve of Eratosthenes
signed main()
{
int n;
cout << "Enter The Number upto which we have to find Prime Numers : ";
cin >> n;
bool Prime[n + 1];
for (int i = 0; i <= n; i++)
{
Prime[i] = true;
}
int a;
Prime[0] = false;
Prime[1] = false;
for (int i = 2; i <= sqrt(n); i++)
{
if (Prime[i] == true)
{
for (int j = i * i; j <= n; j += i)
{
Prime[j] = false;
}
}
}
cout << "\nThe Prime Numbers From 1 To " << n << " are : " << endl;
for (int i = 1; i <= n; i++)
{
if (Prime[i] == true)
{
cout << i << " ";
}
}
cout << endl;
return 0;
}