Given an input n, find out the nth prime.for example if n=5 then output should be 11. If n=7 then output should be 17 .
Please comment if you find any mistake.
public class ComputeNthPrime {
static int testcase1 = 6;
static int testcase2 = 59;
boolean bool =true;
public static void main(String args[]){
ComputeNthPrime
testInstance = new ComputeNthPrime();
int result =
testInstance.computePrime(testcase1);
System.out.println(result);
}
//write your code
here
public int computePrime(int n){
int num=0;
int i=0;
for(int k=2;;k++)
{
int count=0;
for(int j=1;j<=k;j++)
{
if(k%j==0)
count++;
}
if(count==2)
{
i++;
}
if(i==n)
{
num=k;
break;
}
}
return num;
}
}
Please comment if you find any mistake.
No comments:
Post a Comment