Pythagorean triples/Java/Brute force primitives: Difference between revisions

Content added Content deleted
m (Moved the LIMIT declaration but forgot to take the comment with it)
m (a*b = 0 (mod 12) for all triples, very very slight speedup, checking that a*b*c = 0 (mod 60) actually slowed it down)
Line 9: Line 9:
import java.util.TreeSet;
import java.util.TreeSet;
import static java.math.BigInteger.ONE;
import static java.math.BigInteger.*;


public class PythTrip2{
public class PythTrip2{
public static final BigInteger TWO = BigInteger.valueOf(2),
public static final BigInteger TWO = BigInteger.valueOf(2),
B7 = BigInteger.valueOf(7), //B7...B191 are used for skipping non-square "a^2 + b^2"s
B7 = BigInteger.valueOf(7), //B7...B191 are used for skipping non-square "a^2 + b^2"s
B12 = BigInteger.valueOf(12),
B31 = BigInteger.valueOf(31),
B31 = BigInteger.valueOf(31),
B127 = BigInteger.valueOf(127),
B127 = BigInteger.valueOf(127),
Line 80: Line 81:
for(BigInteger b = a.add(ONE);
for(BigInteger b = a.add(ONE);
b.compareTo(peri2) < 0; b = b.add(TWO)){
b.compareTo(peri2) < 0; b = b.add(TWO)){
BigInteger bb = b.multiply(b);
//if a^2 + b^2 is not a perfect square then don't even test for c's
//if a^2 + b^2 is not a perfect square then don't even test for c's
BigInteger aabb = aa.add(bb);
BigInteger aabb = aa.add(b.multiply(b));
if((aabb.and(B7).intValue() != 1) &&
if((aabb.and(B7).intValue() != 1) &&
(aabb.and(B31).intValue() != 4) &&
(aabb.and(B31).intValue() != 4) &&
Line 88: Line 88:
(aabb.and(B191).intValue() != 0)) continue;
(aabb.and(B191).intValue() != 0)) continue;
BigInteger ab = a.add(b);
BigInteger ab = a.add(b);
if(a.multiply(b).mod(B12).compareTo(ZERO) != 0) continue;


for(BigInteger c = b.add(b.and(ONE).equals(ONE)? TWO:ONE);
for(BigInteger c = b.add(b.and(ONE).equals(ONE)? TWO:ONE);