Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

nth Prime added

  • Loading branch information...
commit 82a04290675a4bb22bf798906e62cd7ab73583ab 1 parent 39b463a
@nallachaitu authored
Showing with 37 additions and 1 deletion.
  1. +1 −1  largestPrimeFactor.java
  2. +36 −0 nthPrime.java
View
2  largestPrimeFactor.java
@@ -7,7 +7,7 @@ public boolean isPrime(long num)
long i = 2;
if(num == 1 || num == 0)
return false;
- for(;i<num/2;i++)
+ for(;i<=num/2;i++)
{
if(num % i == 0)
return false;
View
36 nthPrime.java
@@ -0,0 +1,36 @@
+//Program Computes the Nth Prime Number
+
+public class nthPrime
+{
+ public long getnthPrime(int n)
+ {
+ int num = 2 ;
+ int flag = 1;//whether a number is prime or not for particular iteration
+ int count=0;//number of primes upto the current iteration.
+ for(; count !=n;num++,flag=1)
+ {
+ for(int i=2;i<=num/2;i++)
+ {
+ if(num % i == 0)
+ {
+ flag = 0;
+ break;
+ }
+ }
+ if(flag == 1)
+ {
+ count++;
+ }
+ }
+ return num-1;
+
+ }
+
+ public static void main(String[] args)
+ {
+ nthPrime test = new nthPrime();
+ long result = test.getnthPrime(10001);
+ System.out.println("Nth Prime : " + result);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.