-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp086.java
More file actions
33 lines (27 loc) · 1.07 KB
/
p086.java
File metadata and controls
33 lines (27 loc) · 1.07 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
package level04;
import org.junit.Test;
import lib.EulerTest;
public class p086 extends EulerTest {
final int N = 1000000;
/**
* Find the least value of M such that there are over N cuboids with size≤M where the shortest
* distance between two opposite vertices is an integer.
* <p>
* Given a cuboid with longest side M and other sides a and b, the shortest distance between two
* opposite vertices is hypot(a+b, M). For efficiency, compute all (a+b) where this distance is
* an integer, and for each (a+b) compute the number of pairs (a, b) that have that sum.
*/
@Test
public void test() {
int longestSide = 1;
for (int numSolutions = 0; true; longestSide++) {
for (int sumOtherSides : rangeC(2, 2 * longestSide))
if (isSq(sq(sumOtherSides) + sq(longestSide)))
numSolutions += Math.min(sumOtherSides, 2 * longestSide + 2 - sumOtherSides) / 2;
if (numSolutions > N)
break;
}
ans = longestSide;
check(1818);
}
}