-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp095.java
45 lines (38 loc) · 1.18 KB
/
p095.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
37
38
39
40
41
42
43
44
45
package level04;
import java.util.Map;
import org.junit.Test;
import lib.EulerTest;
public class p095 extends EulerTest {
final int N = 1000000;
/**
* Find the smallest number in the longest amicable chain with no numbers exceeding N.
*/
@Test
public void test() {
preSumDivisors(N);
int maxChainLen = 0, maxChainMin = 0;
Map<Integer, Integer> seen = map();
for (int i : rangeC(N)) {
int n = i;
while (n <= N && !seen.containsKey(n)) {
seen.put(n, i);
n = sumProperDivisors(n);
}
if (n > N || seen.get(n) != i)
continue;
int chainStart = n, chainLen = 0, chainMin = Integer.MAX_VALUE;
do {
chainLen++;
if (n < chainMin)
chainMin = n;
n = sumProperDivisors(n);
} while (n != chainStart);
if (chainLen > maxChainLen || chainLen == maxChainLen && chainMin < maxChainMin) {
maxChainLen = chainLen;
maxChainMin = chainMin;
}
}
ans = maxChainMin;
check(14316);
}
}