Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2d4ce1d

Browse filesBrowse files
Nth Prime Number Added
1 parent 75ec542 commit 2d4ce1d
Copy full SHA for 2d4ce1d

File tree

Expand file treeCollapse file tree

2 files changed

+100
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+100
-0
lines changed

‎Nth Prime Number/NthPrime.java

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Java program to the nth prime number using Iteration
2+
import java.util.Scanner;
3+
4+
public class NthPrime {
5+
6+
public static void main(String[] args) {
7+
8+
Scanner sc = new Scanner(System.in);
9+
10+
//Taking input
11+
int nth = sc.nextInt();
12+
13+
int num, count, i;
14+
num=1;
15+
count=0;
16+
17+
//This loop continues until the value
18+
// of the count is less than n.
19+
// If the condition is true then
20+
// it will increase the value of num by 1.
21+
while (count < nth){
22+
num=num+1;
23+
for (i = 2; i <= num; i++){
24+
if (num % i == 0) {
25+
// if the number has a divisor other
26+
// than 1 or itself, we break out of the loop
27+
break;
28+
}
29+
}
30+
//The loop breaks and checks whether i is equal to num.
31+
// If it is so then the value of count is increased by 1
32+
// and then again checks the condition of while loop.
33+
//When the while loop terminates we get our final value in the variable num
34+
if ( i == num){
35+
count = count+1;
36+
}
37+
}
38+
System.out.println("Value of nth prime: " + num);
39+
}
40+
}

‎Nth Prime Number/Sieve.java

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Java program to the nth prime number using Sieve of Eratosthenes
2+
import java.util.ArrayList;
3+
import java.util.Scanner;
4+
5+
class Sieve
6+
{
7+
8+
// initializing the max value
9+
static int MAX_SIZE = 1000005;
10+
11+
// To store all prime numbers
12+
static ArrayList<Integer> primes = new ArrayList<Integer>();
13+
14+
// Function to generate N prime numbers
15+
// using Sieve of Eratosthenes
16+
static void SieveOfEratosthenes()
17+
{
18+
// Create a boolean array "IsPrime[0..MAX_SIZE]"
19+
// and initialize all entries it as true.
20+
// A value in IsPrime[i] will finally be false
21+
// if i is Not a IsPrime, else true.
22+
boolean [] IsPrime = new boolean[MAX_SIZE];
23+
24+
for(int i = 0; i < MAX_SIZE; i++)
25+
IsPrime[i] = true;
26+
27+
for (int p = 2; p * p < MAX_SIZE; p++)
28+
{
29+
// If IsPrime[p] is not changed,
30+
// then it is a prime
31+
if (IsPrime[p] == true)
32+
{
33+
// Update all multiples of p greater than or
34+
// equal to the square of it
35+
// numbers which are multiple of p and are
36+
// less than p^2 are already been marked.
37+
for (int i = p * p; i < MAX_SIZE; i += p)
38+
IsPrime[i] = false;
39+
}
40+
}
41+
42+
// Store all prime numbers
43+
for (int p = 2; p < MAX_SIZE; p++)
44+
if (IsPrime[p] == true)
45+
primes.add(p);
46+
}
47+
48+
// Driver Code
49+
public static void main (String[] args)
50+
{
51+
Scanner sc = new Scanner(System.in);
52+
53+
int N=sc.nextInt();
54+
55+
// Function call
56+
SieveOfEratosthenes();
57+
58+
System.out.println("Nth prime number is " + primes.get(N-1));
59+
}
60+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.