-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path02_raman_and_primes.cpp
83 lines (59 loc) · 1.21 KB
/
02_raman_and_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Problem: Raman and Primes
Raman is learning Sieve of Eratosthenes, He is stuck somewhere. Help him printing prime numbers.
Input Format: Single line containing integral value n.
Constraints: 1<=n<=500000
Output Format: Integral value denoting nth prime number.
Sample Input: 1
Sample Output: 2
*/
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
#define ll long long
int const SIZE = 10000005;
bitset<SIZE> p;
vector<ll> primes;
void sieve()
{
// set all bits
p.set();
// 0 & 1 are not prime
p[0] = p[1] = 0;
for(ll i=2; i<=SIZE; i++)
{
if(p[i]==1)
{
primes.push_back(i);
// lets make all the multiples of i as Not Prime
for(ll j=i*i; j<=SIZE; j=j+i)
{
p[j] = 0;
}
}
}
}
int main()
{
// get all prime values
sieve();
int pos;
cout << "Enter position: ";
cin >> pos;
cout << "Prime No: ";
cout << primes[pos-1] << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter position: 1
Prime No: 2
Case 2:
Enter position: 9865
Prime No: 103123
Case 3:
Enter position: 456
Prime No: 3221
*/