-1

I am writing a program to find the kth prime asked by q queries.

Problem source https://www.spoj.com/problems/TDKPRIME/

My code gets segmentation fault in vscode but got successfully accepted in an online compiler like IDEONE. I don't know the reason for the same. I am attaching the code with given input.

The code is:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define endl "\n"
#define MAX_SIZE 90000001

vector<int> primes;

void sieveOfEratosthenes()
{
    bool isPrime[MAX_SIZE];
    memset(isPrime, true, sizeof(isPrime));

    for (int p = 2; p * p < MAX_SIZE; p++)
    {
        if (isPrime[p] == true)
        {
            for (int i = p * p; i < MAX_SIZE; i += p)
            {
                isPrime[i] = false;
            }
        }
    }
    for (int p = 2; p < MAX_SIZE; p++)
    {
        if (isPrime[p])
            primes.push_back(p);
    }
    return;
}

void solve()
{
    int n;
    cin >> n;
    cout << primes[n - 1] << endl;
    return;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    IOS;

    //Code starts from here //
    sieveOfEratosthenes();
    int q;
    cin >> q;
    while (q--)
        solve();
    // Code end here //
    return 0;
}

I was giving INPUT as :

7
1
10
100
1000
10000
100000
1000000

And the required OUTPUT is:

2
29
541
7919
104729
1299709
15485863

Please help... Thanks in advance

1 Answers1

0

Your OS limits the maximum memory that can be attained by the program, try decreasing the size in

#define MAX_SIZE 90000001

or increase the memory limit on your system and it'll work

RuiSiang
  • 154
  • 6