Anonymous user
Pythagorean triples/Java/Brute force primitives: Difference between revisions
Pythagorean triples/Java/Brute force primitives (view source)
Revision as of 20:45, 20 December 2011
, 12 years agoSomewhere in there I got rid of the need for 12 as a BigInteger, reformat
m (Forgot to update the output, remove runtime check) |
m (Somewhere in there I got rid of the need for 12 as a BigInteger, reformat) |
||
Line 8:
import java.util.Set;
import java.util.TreeSet;
import static java.math.BigInteger.*;
public class PythTrip{
//change this to whatever perimeter limit you want;the RAM's the limit
private static BigInteger
public static class Triple implements Comparable<Triple>{
BigInteger a, b, c, peri;
boolean prim;
public Triple(BigInteger a, BigInteger b, BigInteger c, boolean prim){
this.a = a;
this.b = b;
this.c = c;
peri = a.add(b).add(c);
this.prim = prim;
}
public Triple scale(long k){
return new Triple(a.multiply(BigInteger.valueOf(k)), b
.multiply(BigInteger.valueOf(k)), c.multiply(BigInteger
.valueOf(k)), prim && k == 1);
}
@Override
public boolean equals(Object obj){
if(obj.getClass() != this.getClass())
return false;
Triple trip = (Triple) obj;
return a.equals(trip.a) && b.equals(trip.b) && c.equals(trip.c);
}
@Override
public int compareTo(Triple o){
if(!a.equals(o.a))
return a.compareTo(o.a);
if(!b.equals(o.b))
return b.compareTo(o.b);
if(!c.equals(o.c))
return c.compareTo(o.c);
return 0;
}
public String toString(){
return a + ", " + b + ", " + c + (prim ? " primitive" : "");
}
}
private static Set<Triple> trips = new TreeSet<Triple>();
public static void addAllScales(Triple trip){
long k = 2;
Triple tripCopy = trip.scale(k++);
while(tripCopy.peri.compareTo(LIMIT) <= 0){
trips.add(tripCopy);
tripCopy = trip.scale(k++);
}
}
public static void main(String[] args){
long primCount = 0;
long start = System.currentTimeMillis();
BigInteger peri2 =
for(BigInteger a = B3; a.compareTo(peri3) < 0; a = a.add(ONE)){
BigInteger aa = a.multiply(a);
boolean amod3 = a.mod(B3).equals(ZERO);
boolean amod4 = a.mod(B4).equals(ZERO);
//b is the opposite evenness of a so increment by 2
for(BigInteger b = a.add(ONE); b.compareTo(peri2) < 0; b = b
//if a^2+b^2 isn't a perfect square, don't even test for c's
BigInteger aabb = aa.add(b.multiply(b));
if((aabb.and(B7).intValue() != 1)
&& (aabb.and(B31).intValue() != 4)
&& (aabb.and(B127).intValue() != 16)
&& (aabb.and(B191).intValue() != 0))
if(!a.gcd(b).equals(ONE))
continue;
BigInteger ab = a.add(b);
// c is always odd for primitives so
// otherwise
for(BigInteger c = b.add(b.testBit(0) ? ZERO :
// if a+b+c > periLimit
if(ab.add(c).compareTo(LIMIT) > 0) break;
int compare = aabb.compareTo(c.multiply(c));
// if a^2 + b^2 != c^2
if(compare < 0){
break;
}else if
Triple prim = new Triple(a, b, c, true);
if(trips.add(prim)){
primCount++;
addAllScales(prim);
}
}
Line 122 ⟶ 123:
}
}
for(Triple trip : trips){
System.out.println(trip);
}
System.out.println("Runtime: " + (System.currentTimeMillis() - start));
System.out.println("Up to a perimeter of " + LIMIT + ", there are "
+ trips.size() + " triples, of which " + primCount
+ " are primitive.");
}
}</lang>
|