0
vector<int> Solution::primesum(int n) {
  int prime[n+1];
  for(int i=0;i<n+1;i++){
    prime[i]=1;
  }
   prime[0]=0;
   prime[1]=0;
   for(int i=2;i<=sqrt(n);i++){

        if(prime[i]==1){
            for(int j=2;i*j<=n;j++)
               prime[i*j]=0;
        }

  }
  vector <int> sum;
  for(int i=2;i<=(n)/2;i++){
      if(prime[i]==1&&prime[n-i]==1){
       sum.push_back(i);
       sum.push_back(n-i);
       return sum;
      }
      }
    return sum;
     } 

The above code is working fine for smaller inputs but when I try to provide it with numbers something like >100000000, it is showing segmentation fault.Can someone please help me? Thanks!

1 Answers1

0

Most likely int prime[n+1] leads to a stackoverflow (100'000'000 requires ~400 MB on the stack). I'd recommend using std::vector<int> prime(n+1) instead.

MikeMB
  • 20,029
  • 9
  • 57
  • 102
  • Thanks but that was also showing the same error. – Namrata Tanwar Jul 05 '17 at 02:17
  • @NamrataTanwar: Can't reproduce your problem in this case. Can you open a new question with your new code, including information about your platform, compiler, flags a.s.o. – MikeMB Jul 05 '17 at 09:36