-6

I am using sieve of eratosthenes to solve this problem but it is giving me SIGABRT error although my code is working fine on codeblocks.... Please help me modify this code to remove error....

My code is...

#include<vector>
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    unsigned long int t, n, m,i,j;
    vector<int> prime;
    cin>>t;
    while(t--)
    {
        cin>>m;
        cin>>n;
        while(!(1<=m&&m<=n&&n<=1000000000&&n-m<=100000))
            cin>>m>>n;

        prime.resize(n);

        for(i=0;i<n;i++)
            prime[i]=1;

        prime[0]=0;
        prime[1]=0;

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

        for(i=m;i<=n;i++)
        {
            if(prime[i]==1)
                cout<<i<<endl;
        }
        cout<<endl;
        prime.resize(0);

    }
    return 0;
}
Archit
  • 9
  • 4
  • It would be easier to help if your question included the values you used for t, m, and n, as well as the output from `std::cout`, and any text around the `SIGABRT` report. – donjuedo Jul 24 '15 at 18:57
  • I submitted this solution on codechef during which this SIGABRT error, there was no extra relevant text around it – Archit Jul 24 '15 at 19:09
  • This question may be of some help: [When does a process get a SIGABRT?](http://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6) – Thomas Matthews Jul 24 '15 at 19:15

2 Answers2

1

Your j loop allows i*j to equal n, but the vector of size n must be indexed from 0 to n-1. The existing code permits referencing an element out of bounds.

The same problem can occur in the last loop, too.

donjuedo
  • 2,475
  • 18
  • 28
  • I tried to modify the code as per your suggestion but still SIGABRT error appears.......... https://www.codechef.com/viewsolution/7537449 – Archit Jul 24 '15 at 19:09
0

The SIGABRT is issued by library routines.
You have two library routines in your program: std::vector and sqrt.

Either assign sqrt(n) to a const variable or replace the condition with:
(i * i) < n;

You need to verify that:
prime[i*j]
is a valid location. In other words, (i * j) < n.

Some information about primes (that can help you code your program):

  • Prime numbers are odd except the value 2.
  • Your test value can start at 3 and add 2 to get to the next value.
  • You may be able to save some time by looking values in an array of known values.
  • Multiplication is usually faster than division. Try rewriting your test to use multiplication and not division.
  • Use a data type that can contain the maximum value.
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I tried to modify the code and changed vector to arrays but now SIGSEGV error appers reason of which I can't find.......... please help ........ https://www.codechef.com/viewsolution/7537435 – Archit Jul 25 '15 at 06:30
  • Post a new question containing your modified code. Also, search StackOverflow for "c++ code chef prime number". – Thomas Matthews Jul 26 '15 at 16:58
  • Also, use a debugger to find the line that is causing the issue. – Thomas Matthews Jul 26 '15 at 16:58