-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem044.java
More file actions
52 lines (52 loc) · 1.63 KB
/
Problem044.java
File metadata and controls
52 lines (52 loc) · 1.63 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
import java.util.HashMap;
public class Problem44 {
private static HashMap<Long, Long> pents;
private static long n;
private static void addPent() {
n++;
pents.put(n, (n * (3 * n - 1) / 2));
}
private static boolean isPent(long l) {
if (((1 + Math.sqrt(24 * l + 1)) / 6) % 1 == 0) {
return true;
}
return false;
}
public static void main(String[] args) {
long lowN = 1;
pents = new HashMap<Long, Long>();
n = 0;
addPent();
addPent();
long smallestDiff = Long.MAX_VALUE;
long[] nums = new long[2];
long pentDiff = 0;
long index = 2;
while (pentDiff < smallestDiff) {
while (smallestDiff < (pents.get(index) - pents.get(lowN))) {
pents.remove(lowN);
lowN++;
}
addPent();
pentDiff = pents.get(index + 1) - pents.get(index);
boolean found = false;
long i = index - 1;
while (!found && i >= lowN) {
if (isPent(pents.get(index) + pents.get(i))) {
long diff = pents.get(index) - pents.get(i);
if (isPent(diff)) {
found = true;
if (diff < smallestDiff) {
nums[0] = pents.get(index);
nums[1] = pents.get(i);
smallestDiff = diff;
}
}
}
i--;
}
index++;
}
System.out.println(nums[0] - nums[1]);
}
}