Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long long pr[15001];
- void SieveOfEratosthenes(long long n)
- {
- // Create a boolean array "prime[0..n]" and initialize
- // all entries it as true. A value in prime[i] will
- // finally be false if i is Not a prime, else true.
- bool prime[n+1];
- memset(prime, true, sizeof(prime));
- for (long long p=2; p*p<=n; p++)
- {
- // If prime[p] is not changed, then it is a prime
- if (prime[p] == true)
- {
- // Update all multiples of p
- for (long long i=p*2; i<=n; i += p)
- prime[i] = false;
- }
- }
- //Storing all prime numbers in a single array
- for (long long p=2, j=1; p<=n; p++)
- if (prime[p]) {
- pr[j]=p;
- j++;
- }
- }
- // Driver Program to test above function
- int main()
- {
- int t, n;
- SieveOfEratosthenes(164000);
- cin>>t;
- while (t--) {
- cin>>n;
- cout<<pr[n]<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement