Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, * we can see that the 6th prime is 13. * What is the 10001st prime number? #include<stdio.h> #include<math.h> #define FIRST_NUM 2 #define START_COUNT 0 void prime(int num, int count) int i, flag=1; for(i=2; i<=sqrt(num); i++) if(num%i==0) flag=0; break; flag=1; if(flag) count++; if(count==10001) printf("The 10001st prime number is %d\n", num); if(count!=10001) prime(num+1, count); int main(void) prime(FIRST_NUM, START_COUNT); return 0; // Answer: 104743

I used both CodeBlocks and Eclipse on both the platforms.

It is running fine in Linux, in CodeBlocks , with:

The 10001st prime number is 104743
Process returned 35 (0x23) execution time : 0.0109 s

But not in Windows.

However, when I tried to debug it in CodeBlocks (in Windows) it gave me this error :

Program received signal SIGSEGV, Segmentation fault
and call stack as: Nr= #0 | Address= 77101566 | Function= msvcrt!modf() | File= (C:\WINDOWS\SysWOW64\msvcrt.dll:??) Nr= #1 | Address= ?? | Function= ?? () | File= (??:??) //I don't think this one is important :)

I tried to search on Internet about SIGSEGV errors but couldn't get satisfied.

Please explain as much you can. I will be really grateful. :)

A segmentation fault most probably points to undefined behavior, which can cause expected behavior as well. So, if it works under one OS but not on the other, you cannot conclude that the correctness of the code is OS-dependent. cadaniluk Jan 29, 2016 at 17:11 why do this recursively? there is nothing in there that couldn't be done with regular iteration. you don't return any values, you're not changing anything, you just dive down 10,000ish times and then exit, so the recursiion is essentially useless. Marc B Jan 29, 2016 at 17:13 @cad Alright, please tell me what is wrong here, really? I think this is a very basic code with no heavy stuff involved. Sachin Jan 29, 2016 at 17:15 Stack overflow, I'm sure. The recursion is blowing your stack on Windows. Apparently your Linux build has more stack space. Fred Larson Jan 29, 2016 at 17:16 I did a quick mod to your program to determine how deep the recursion goes. It recurses 104741 calls deep. That's a lot. It could be a stack space issue at least on Windows. On Linux it may be an issue but may not have manifested as a segmentation fault. Adding to @MarcB 's point, using recursion isn't just a matter of taste. It's a poor way to implement this algorithm and is likely causing you your problem. lurker Jan 29, 2016 at 17:24

the following code cleanly compiles, produces the expected output, does not use recursion, greatly reduces the amount of the flag value thrashing, separates the calculation algorithm from the display of the result, was tested on linux 14.04

// 10001st prime
 * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
 * we can see that the 6th prime is 13.
 * What is the 10001st prime number?
#include <stdio.h>
#include <math.h>
#define FIRST_NUM  (2)
#define START_COUNT (0)
#define WHICH_PRIME (10001)
int prime( void )
    int i;
    int num = FIRST_NUM;
    for( int count = 0; count < WHICH_PRIME; )
        int flag=1;
        int j = (int)sqrt((double)(num));
        for(i=2; i<=j; i++)
            if( !(num%i) )
                flag=0;
                break;
        if(flag)
            count++;
            if(count==10001)
                break;
        num++;
    return num;
int main(void)
    int result = prime();
    printf("The 10001st prime number is %d\n", result);
    return 0;
// Answer: 104743
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.