forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp003.java
More file actions
46 lines (38 loc) · 1.33 KB
/
p003.java
File metadata and controls
46 lines (38 loc) · 1.33 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
/*
* Solution to Project Euler problem 3
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p003 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p003().run());
}
/*
* By the fundamental theorem of arithmetic, every integer n > 1 has a unique factorization as a product of prime numbers.
* In other words, the theorem says that n = p_0 * p_1 * ... * p_{m-1}, where each p_i > 1 is prime but not necessarily unique.
* Now if we take the number n and repeatedly divide out its smallest factor (which must also be prime), then the last
* factor that we divide out must be the largest prime factor of n. For reference, 600851475143 = 71 * 839 * 1471 * 6857.
*/
public String run() {
long n = 600851475143L;
while (true) {
long p = smallestFactor(n);
if (p < n)
n /= p;
else
return Long.toString(n);
}
}
// Returns the smallest factor of n, which is in the range [2, n]. The result is always prime.
private static long smallestFactor(long n) {
if (n <= 1)
throw new IllegalArgumentException();
for (long i = 2, end = Library.sqrt(n); i <= end; i++) {
if (n % i == 0)
return i;
}
return n; // n itself is prime
}
}