How do you even...

Find the 2014th prime number.

Example: The 2nd prime number is 3 and the 3rd prime number is 5.


The answer is 17497.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Ronak Agarwal
Sep 5, 2014

Java code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int sum = 0 ;
    int i = 2 ;
    for(;;){
    if(isprime(i))
        sum=sum+1 ;
    if(sum==2014)
        break ;
    i=i+1 ;

    }
    System.out.print(i) ;

For checking whether a number is prime I have created a method isprime whose code is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static boolean isprime(long n){
   if(n==2)
       return true ;
   else
   {int k = (int) Math.pow(n,0.5) ;
   if(n%2==0)
       return false ;
   else
   {for(int s=3;s<k+1;s=s+2){
   if(n%s==0)
       return false ;}
   return true ;}

   }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
 int a,b,x,y,i,j;
    cin>>x;
    cin>>y;
    a=0;
    for(i=2;i<x;i++)

    {  b=0;
        for(j=2;j<i;j++)
     {      if(i%j!=0)
              b++ ;
     }
         if(b==i-2)
         a++;
             if(a==y)
             break;
    }
     cout<<i;
    return 0;
} 

Where y = 2014 y=2014 and x x is a sufficiently large number(in this case, x > 20000 x>20000 .

Siddharth G - 6 years, 8 months ago
Bill Bell
Dec 5, 2014

I should be ashamed. Using the Python sympy library.

I have already written prime number code several times though.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...