SIGSEGV error

problem: http://www.codechef.com/problems/PRIMES2

solution: http://www.codechef.com/viewsolution/2329374

plz rectify this…

In your solution there is a line-

if((p3=i*i*i<n)&& isprime(i)){

Here p3=i*i*i<n is wrong, you are thinking that p3 is assigned i*i*i, but instead it is assigned i*i*i<n, as comparison(<) has higher precedence than assignment(=).

Now what happens is : p3 gets value 0 or 1, based upon the comparison : i*i*i<n

In line

p1=(n-p3)-j*j;

Incorrect value of p3 is used. Plus you need to check value of j so that p1 does not become negative. As isprime(p1) is accessing array, a negative offset will cause SIGSEGV.

To fix this, put parenthesis to force assignment first - ((p3=i*i*i)<n), and fix the loop iterating over j.

For general information on SIGSEGV read my answer here.

2 Likes

Got that…
but still giving SIGSEGV here… i don’t know why?.. http://www.codechef.com/viewsolution/2330738
working fine on ideone… http://ideone.com/9JpRfh

//