-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path10001stPrime.java
67 lines (53 loc) · 1.51 KB
/
10001stPrime.java
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
61
62
63
64
65
66
67
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int[] store = new int[T];
int j, max = 0;
for(int i = 0; i < T; i++)
{
j = sc.nextInt();
store[i] = j-1;
if(j > max)
max = j;
}
int[] primeChecker = new int[max];
int[] primeNumbers = new int[max];
int end = 1;
primeChecker[0] = 2;
primeNumbers[0] = 2;
primeNumbers = findPrime(primeChecker, primeNumbers, end, max);
for(int i = 0; i < T; i++)
{
System.out.println(primeNumbers[store[i]]);
}
}
public static int[] findPrime(int[] primeChecker, int[] primeNumbers, int end, int size)
{
int i = 2;
boolean cond;
while(end < size)
{
i++;
cond = true;
for(int j =0; j < end; j++)
{
primeChecker[j]--;
if(primeChecker[j] == 0)
{
primeChecker[j] = primeNumbers[j];
cond = false;
}
}
if(cond)
{
primeChecker[end] = i;
primeNumbers[end] = i;
end++;
}
}
return primeNumbers;
}
}