-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler007.java
More file actions
executable file
·64 lines (54 loc) · 1.5 KB
/
Copy pathEuler007.java
File metadata and controls
executable file
·64 lines (54 loc) · 1.5 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
61
62
63
64
import java.util.Date;
/*
Project Euler Problem 7
=======================
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 10001st prime number?
*/
public class Euler007 {
public static void main(String[] args) {
Date start, end;
start = new Date();
long prime = 0;
int counter = 0;
long primeCandidate = 0;
while (counter < 10001) {
if (isPrime(primeCandidate)) {
if (counter == 10000) {
prime = primeCandidate;
break;
}
counter++;
}
primeCandidate++;
}
System.out.println(prime);
end = new Date();
System.out.println("Execution Time: " + (end.getTime() -start.getTime()));
}
public static boolean isPrime(long num) {
if (num == 0 || num == 1)
return false;
if (num < 4)
return true;
if (num % 2 == 0)
return false;
if (num < 9)
return true;
if (num % 3 == 0)
return false;
else {
int sqrt = (int) Math.floor(Math.sqrt(num));
int f = 5;
while (f <= sqrt) {
if (num % f == 0)
return false;
if (num % (f + 2) == 0)
return false;
f += 6;
}
return true;
}
}
}