0

This is a problem on spoj.com (http://www.spoj.com/problems/PRIC/) .We have to check whether numbers of the sequence : ai=( a(i-1)+1234567890 ) mod 2^31 are prime or not, 1st number is 1. My code is given below (Please try to ignore the clumsiness.) based on sieve of eratosthenes .

The PROBLEM : We have to print "prime(1) or not(0)" for sequence upto i=33,333,333 , My code works perfectly fine for i( c3 in the code) values upto 8000 or so and after that (e.g c3>19000 ) it starts giving The SIGFPE error . Now i googled about the error , it has something to do with division/ mod by 0. But why is it that code works for c3 values upto 9000 but not beyond that?

guitar_geek
  • 498
  • 2
  • 9
  • 19
  • 1
    Have you tried running the program in the debugger? `gdb` will usually give you a trace of the fault if you compile with debug symbols :) – Morten Jensen May 28 '14 at 18:18
  • I have no idea what that means , could you point to a useful link maybe ? – guitar_geek May 28 '14 at 18:21
  • Why not use a websearch if you want to find information? Why should we do it for you? Why should we have to search for links for you? If you want to write code you will need to take time to learn how to debug. – David Heffernan May 28 '14 at 18:25
  • Perhaps `prime2[j]` is zero. – BLUEPIXY May 28 '14 at 18:34
  • @BLUEPIXY you seem to be right good Sir, see my answer :) – Morten Jensen May 28 '14 at 18:38
  • @DavidHeffernan .. i rarely post questions . only if I have tried a lot and can't understand, I have already put a lot of time into this and wasn't able to understand what was happening here, that's why i asked . What am I to do now, search google endlessly until the end of time ? Experienced users here might identify the problem in an instant , so i came here for help. – guitar_geek May 28 '14 at 18:40
  • My comment was in response to yours. You do need to learn how to search, and you do need to learn debugging skills. Which line of code throws the error? – David Heffernan May 28 '14 at 18:41
  • Having never used debugging features , the google results went over my head , that's why i asked for a specific link. :) I'm checking out the answer now , given below by @Morten ... – guitar_geek May 28 '14 at 18:45
  • @user2657257 which compiler/IDE are you using? Try checking Google and Youtube for quick tutorials on using a debugger – Morten Jensen May 28 '14 at 18:48
  • getting `SIGFPE` from integer operations usually means you did a divide-by-zero. The modulus operator involves division, so I'd look at `a%prime2[j]` - check that `prime2[j]` is nonzero first. Another possibility is integer overflow causing UB (I didn't check in detail to see if this actually happens or not). – M.M May 28 '14 at 22:50

1 Answers1

6

Depending on your compiler and development environment, you should read up on the concept of a Debugger. This answer has a guide to use gdb. If you are using Visual Studio, Code::Blocks or any other IDE, look up the debugging features. For instance how to set a breakpoint or step into/out of/over a function call for instance, watching or changing variables etc. (I'm mentioning these things to give you vital hints for Google search-words, wink wink nudge nudge).

EDIT:

Copy-pasted the code and saved it, compiled with gcc -g for debug symbols and -lm to link the math library, I ran it through gdb and it gave me this output:

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400707 in sieve (prime=0x6626a0) at t.c:43
43        if (a%prime2[j]==0){

This tells you to look at line 43, at the if statement that uses a modulo operation. This seems to be the place you are doing the modulo zero.

Do note that line 43 in the document I got when I copy-pasted your code from Stackoverflow, may not be line 43 in your document.

EDIT2:

Hey my answer was unaccepted! - why was that :) ?

Community
  • 1
  • 1
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • that was really informative . cool :) lemme check that in my code – guitar_geek May 28 '14 at 18:41
  • 3
    Look into using a debugger. The runtime environment you have in C is not much to speak of. In my opinion you need all the help you can get from tooling when developing in C. – Morten Jensen May 28 '14 at 18:43
  • 3
    @user2657257 Please take Morten's excellent advice and reproduce the problem in your debugger instead of just applying the fix. You will be amazed at the new powers you will unleash! – Steger May 28 '14 at 18:47
  • Got it.. really stupid error though :) thanks .. it should be j

    – guitar_geek May 28 '14 at 19:59