forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSieve.java
More file actions
60 lines (49 loc) · 1.72 KB
/
Sieve.java
File metadata and controls
60 lines (49 loc) · 1.72 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Java program to the nth prime number using Sieve of Eratosthenes
import java.util.ArrayList;
import java.util.Scanner;
class Sieve
{
// initializing the max value
static int MAX_SIZE = 1000005;
// To store all prime numbers
static ArrayList<Integer> primes = new ArrayList<Integer>();
// Function to generate N prime numbers
// using Sieve of Eratosthenes
static void SieveOfEratosthenes()
{
// Create a boolean array "IsPrime[0..MAX_SIZE]"
// and initialize all entries it as true.
// A value in IsPrime[i] will finally be false
// if i is Not a IsPrime, else true.
boolean [] IsPrime = new boolean[MAX_SIZE];
for(int i = 0; i < MAX_SIZE; i++)
IsPrime[i] = true;
for (int p = 2; p * p < MAX_SIZE; p++)
{
// If IsPrime[p] is not changed,
// then it is a prime
if (IsPrime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i < MAX_SIZE; i += p)
IsPrime[i] = false;
}
}
// Store all prime numbers
for (int p = 2; p < MAX_SIZE; p++)
if (IsPrime[p] == true)
primes.add(p);
}
// Driver Code
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int N=sc.nextInt();
// Function call
SieveOfEratosthenes();
System.out.println("Nth prime number is " + primes.get(N-1));
}
}