-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path007: 10001st prime.java
76 lines (72 loc) · 2.19 KB
/
007: 10001st prime.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
68
69
70
71
72
73
74
75
76
import java.io.*;
import java.util.*;
public class Solution {
public boolean[] primePE() {
long l=100000000l;
int cl = (int)Math.sqrt((double)l);
boolean s[] = new boolean[(int)(l+1)];
Arrays.fill(s, false);
for (int i=4;i<=l;i=i+2) {
s[i]=true;
}
for (int i=3;i<=cl;i=i+2) {
if (!s[i]) {
for (int j=i*i;j<=l;j=j+(2*i)) {
s[j]=true;
}
}
}
return s;
}
public void findLastPrime(boolean s[], long limit) {
int count = 0, lastp=1;
for (int i=2;i<=s.length;i++){
if(!s[i]) {
count++;
//System.out.println(i + " " + count);
if (count==limit) {
lastp= i;
//System.out.println(i);
break;
}
}
}
System.out.println(lastp);
}
public boolean isPrime(long n) {
if(n==1)
return false;
else if (n<4)
return true; //2 and 3 are prime
else if (n%2==0)
return false;
else if (n<9)
return true; //we have already excluded 4,6 and 8.
else if (n%3==0)
return false;
else {
double r=Math.floor(Math.sqrt(n)); // n rounded to the greatest integer r so that r*r<=n
double f=5;
while(f<=r) {
if (n%f==0)
return false;// (and step out of the function)
if (n %(f+2)==0)
return false; //(and step out of the function)
f=f+6;
}
}
return true; //(in all other cases)
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Solution p = new Solution();
boolean primes[] = p.primePE();
Scanner s=new Scanner(System.in);
int n = s.nextInt();
long l;
while(s.hasNext()) {
l = s.nextLong();
p.findLastPrime(primes, l);
}
}
}