-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sieve of Eratosthenes(Store Primes).cpp
67 lines (50 loc) · 1.35 KB
/
Sieve of Eratosthenes(Store Primes).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
/***
Bismillahir Rahmanir Rahim
Read the name of Allah, who created you!!!
Author : Shah Newaj Rabbi Shishir
Department of CSE, City University, Bangladesh.
***/
#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
int num = 1500001;
bool check[1500001];
vector <int> v;
void sieve ()
{
v.pb (2);
int i,j;
check[0] = check[1] = true;
for (i=4; i<=num; i+=2)
check[i] = true;
for (i=3; i*i<=num; i+=2)
if (check[i] == false)
for (j=i*i; j<num; j+=2*i)
check[j] = true;
for (i=3; i<num; i+=2)
if (check[i] == false)
v.pb(i);
}
int main (void)
{
sieve ();
int n,len=v.size(),i;
cout << "Total primes from 1 to 1500001 : " << len << endl;
cout << "Last prime : " << v[len-1] << endl;
cout << "Last 50 primes :\n";
for (i=len-1; i>=len-50; i--)
cout << v[i] << endl;
return 0;
}