-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathProblem_7.java
More file actions
48 lines (30 loc) · 765 Bytes
/
Problem_7.java
File metadata and controls
48 lines (30 loc) · 765 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
*/
class Problem_7 {
public static boolean isPrime(long n) {
if (n < 2) {return false;}
else if (n == 2) {return true;}
for (int i = 3; i < Math.sqrt(n) + 1; i+=2)
{ if (n % i == 0)
{return false;}
}
return true;
}
public static void main(String[] args) {
int a=1;
for(int i=3;i>0;i= i+=2)
{
if(isPrime(i)==true)
{
a++;
}
if(a==10001)
{
System.out.println(i);
break;
}
}
}
}