-3

WHY RUN TIME ERROR?..please help i am trying to output the ith prime number but i keep gettng run time error.i can run the code in an offline compiler but not in a online compiler

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

vector<long long>prima;
long long int SieveOfEratosthenes(){
    bool prime[1000006];
    for (long long int p = 2; p <= 1000000; p++) {
        if (prime[p]==false){
        prima.push_back(p);
            for (long long int i = p * p; i <= 1000000; i += p){
                prime[i] = true;
            }                
        }   
    }
}     

int main(){ 
    long t;
    cin>>t;
    SieveOfEratosthenes();
    while(t--){
        long long int k;
        cin>>k;
        cout<<prima[k-1];
    }
    return 0;
}
  • Side notes: (1) [#include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) (2) [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – wohlstad Jun 01 '23 at 05:03
  • `bool prime[1000006];` this is too large array for a relative small stack. Reduce the array. – 273K Jun 01 '23 at 05:04
  • Where are you learning C++ from? `#include using namespace std;` is an indication its not a good source. You also should return something from your sieve function and use a debugger to find some bugs in your logic too. Replace your "array" with std::vector, since your array is too big to fit on the stack. – Pepijn Kramer Jun 01 '23 at 05:05
  • 2
    I dunno, but your `SieveOfEratosthenes` doest' actually return a value. Hence, it doesn't compile. – selbie Jun 01 '23 at 05:05
  • 1
    Please confirm that a) the code you are using and which exhibits the problem you describe is the code shown here, i.e. a [mre] b) it can be compiled and run c) it has a runtime error, i.e. an error occurring while running it d) not a TLE ("time limit exceeded"), i.e. a failure to satisfy an online-judge, because of taking too long e) you wrote the code – Yunnosch Jun 01 '23 at 05:58
  • [C/C++ the result of the uninitialized array](https://stackoverflow.com/questions/39351746/c-c-the-result-of-the-uninitialized-array) and [Segmentation fault on large array sizes](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – Jason Jun 01 '23 at 06:13

1 Answers1

1

This declaration

bool prime[1000006];

Leaves the values in prime undefined including trap values that will cause undefined behavior. Even if you fix that, it risks stack overflow for being so large.

So just replace the above line with:

std::vector<bool> prime(1000006);

Aside from a #include <vector> at the top of the file, everything else remains the same.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • You haven't addressed the fact that a function declared as returning a `long long int` doesn't actually return *anything*. – Adrian Mole Jun 02 '23 at 05:56
  • @AdrianMole I called that out in the comments below the OP's question. Seeing how the code wouldn't (likely) compile unless this was addressed... – selbie Jun 02 '23 at 07:15