Problem 7

Statement
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?

Solution
The code, in C.

#include <stdio.h>

int prime(int n)
{
  int a[150000];
  unsigned long int i,j;
  for(i=0;i<150000;i++)
    a[i]=1;
  a[0]=0;
  a[1]=0;
  i=2;
  while(i<400){
    for(j=i+1;j<150000;j++)
      if(!(j%i)){
	a[j]=0;
      }
    for(j=i+1;j<150000;j++){
      if(a[j]==1) break;
    }
    i=j;
  }
  j=0;
  for(i=0;i<150000;i++){
    if(a[i])
      j++;
    if(j==n){
      return i;
      break;
    }
  }
  return 0;
}

int main(void)
{
  printf("%d\n",prime(1001));
  return 0;
}

Answer
104743

No tags for this post.

SPEAK / ADD YOUR COMMENT
Comments are moderated.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>