-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem7.java
More file actions
31 lines (31 loc) · 908 Bytes
/
Copy pathProblem7.java
File metadata and controls
31 lines (31 loc) · 908 Bytes
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
// Author @mr.gmiak.dv
public class Problem7 {
public static void main(String[] args) {
int n = 10001;
System.out.println("What is the "+n+"th prime number? "+ nthPrime(n)+".");
}
public static int nthPrime(int n) {
int nthPrime=0, position=0, start=1;
while (n>position){
start++;
if (isPrime(start)==1) {
position++;
}
}
nthPrime = start;
return nthPrime;
}
// Checking the primality of a given integer x by Trial division.
// Trial division : This method divides x by each integer from 2 up to the square root of x.
public static long isPrime(long x){
int flag=1;
long m=(long)Math.sqrt(x);
for (int i=2; i<=m; i++) {
if (x%i==0){
flag=0;
break;
}
}
return flag;
}
}