-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem003.java
36 lines (32 loc) · 929 Bytes
/
Problem003.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
/**
* 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 Problem003 {
private static long largestPrimeFactor(long n) {
long largestFactor = 1;
if (n%2 == 0) {
largestFactor = 2;
}
for (long p=3; p*p<n; p+=2) {
if (n%p == 0 && isPrime(p)) {
largestFactor = p;
}
}
return largestFactor;
}
private static boolean isPrime(long n) {
// n is always an odd number >= 3
for (int i=3; i*i<n; i+=2) {
if (n%i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("The largest prime factor of the number 600851475143 is:");
System.out.println(largestPrimeFactor(600851475143L));
}
}