-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler003.java
More file actions
executable file
·57 lines (45 loc) · 1.35 KB
/
Copy pathEuler003.java
File metadata and controls
executable file
·57 lines (45 loc) · 1.35 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
import java.util.Date;
/*
Project Euler Problem 3
=======================
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?
*/
public class Euler003 {
public static void main(String[] args) {
Date start, end;
start = new Date();
double number = 600851475143d;
long largestPrime = 0;
for (int i = 3; i < (int) Math.sqrt(number) + 1; i += 2) {
if (number % i == 0) {
if(isPrime((int)number/i)){
largestPrime = (int)number/i;
break;
}
if (isPrime(i) && i > largestPrime) {
largestPrime = i;
}
}
}
end = new Date();
System.out.println(largestPrime);
System.out.println("Execution Time: "
+ (end.getTime() - start.getTime()));
}
public static boolean isPrime(long num) {
double sqrt = Math.sqrt(num);
int sqrtCeil = (int) Math.ceil(sqrt) + 1;
if (num == 0 || num == 1)
return false;
if (num == 2)
return true;
if (num % 2 == 0)
return false;
for (int i = 3; i < sqrtCeil; i += 2) {
if ((num % i) == 0)
return false;
}
return true;
}
}