Last active
December 27, 2017 02:42
-
-
Save zhuker/f3398cbda9e294072084e56156bdc14b to your computer and use it in GitHub Desktop.
EulerBrick brute force
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vg.ts; | |
import static org.junit.Assert.assertTrue; | |
import java.util.stream.LongStream; | |
import org.junit.Test; | |
//http://mathforum.org/dr.math/faq/formulas/faq.parallelepiped.html | |
//https://stackoverflow.com/questions/15212533/java-square-root-integer-operations-without-casting | |
public class PerfectBrickTest { | |
private static final long MIN = 10000000; | |
private static final long MAX = Integer.MAX_VALUE; | |
@Test | |
public void testParallel() throws Exception { | |
LongStream.range(MIN, MAX).parallel().forEach(a -> { | |
System.out.println(System.currentTimeMillis() + " " + a); | |
long a2 = a * a; | |
for (long b = a; b < MAX; b++) { | |
long b2 = b * b; | |
long a2b2 = a2 + b2; | |
if (isPerfectSquare(a2b2)) { | |
// System.out.printf("integer face ab diagonal at a: %d b: %d\n", a, b); | |
} else { | |
continue; | |
} | |
for (long c = b; c < MAX; c++) { | |
long c2 = c * c; | |
long a2c2 = a2 + c2; | |
long b2c2 = b2 + c2; | |
long d2 = a2 + b2 + c2; | |
if (isPerfectSquare(d2)) { | |
// System.out.printf("integer inner diagonal at a: %d b: %d c: %d\n", a, b, c); | |
} else { | |
continue; | |
} | |
if (isPerfectSquare(a2c2)) { | |
// System.out.printf("integer face ac diagonal at a: %d b: %d c: %d\n", a, b, c); | |
} else { | |
continue; | |
} | |
if (isPerfectSquare(b2c2)) { | |
System.out.printf("integer face bc diagonal at a: %d b: %d c: %d\n", a, b, c); | |
System.out.println("FOUND!!!"); | |
throw new RuntimeException("FOUND: " + a + " " + b + " " + c); | |
} | |
} | |
} | |
}); | |
} | |
private static boolean isPerfectSquare(long x) { | |
long tmp = (long) Math.sqrt(x); | |
return x == tmp * tmp; | |
} | |
@Test | |
public void testPerfectSquare() throws Exception { | |
assertTrue(isPerfectSquare(4L)); | |
assertTrue(isPerfectSquare(9L)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment