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. :)
–
–
–
–
–
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.