Pythagorean triples: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: semi-optimized code)
m (→‎{{header|Java}}: Added optimizations inspired by C, hey cool my IDE at home converted tabs to spaces for me)
Line 63: Line 63:
=={{header|Java}}==
=={{header|Java}}==
[[Category:Arbitrary precision]]Theoretically, this can go "forever", but it takes a while, so only the minimum is shown. Luckily, <code>BigInteger</code> has a GCD method built in.
[[Category:Arbitrary precision]]Theoretically, this can go "forever", but it takes a while, so only the minimum is shown. Luckily, <code>BigInteger</code> has a GCD method built in.
<lang java>import java.math.BigInteger;
<lang java>
import java.math.BigInteger;
import static java.math.BigInteger.ONE;


public class PythTrip{
public class PythTrip{
public static void main(String[] args){
long tripCount = 0, primCount = 0;


public static void main(String[] args){
//change this to whatever perimeter limit you want;the RAM's the limit
long tripCount = 0, primCount = 0;
BigInteger periLimit = new BigInteger("100");


//change this to whatever perimeter limit you want;the RAM's the limit
for(BigInteger a = BigInteger.ONE;
BigInteger periLimit = new BigInteger("10000"),
a.compareTo(periLimit) < 0; a = a.add(BigInteger.ONE)){
peri2 = periLimit.divide(new BigInteger("2")),
peri3 = periLimit.divide(new BigInteger("3"));


for(BigInteger b = new BigInteger(a.toString());
for(BigInteger a = ONE; a.compareTo(peri3) < 0; a = a.add(ONE)){
BigInteger aa = a.multiply(a);
b.compareTo(periLimit) < 0; b = b.add(BigInteger.ONE)){
for(BigInteger b = new BigInteger(a.toString()).add(ONE);
b.compareTo(peri2) < 0; b = b.add(ONE)){
BigInteger bb = b.multiply(b);
BigInteger ab = a.add(b);
BigInteger aabb = aa.add(bb);
for(BigInteger c = new BigInteger(b.toString()).add(ONE);
c.compareTo(peri2) < 0; c = c.add(ONE)){


int compare = aabb.compareTo(c.multiply(c));
for(BigInteger c = new BigInteger(b.toString());
//if a+b+c > periLimit
c.compareTo(periLimit) < 0; c = c.add(BigInteger.ONE)){
if(ab.add(c).compareTo(periLimit) > 0){
break;
}
//if a^2 + b^2 != c^2
if(compare < 0){
break;
}else if (compare == 0){
tripCount++;
System.out.print(a + ", " + b + ", " + c);


//does binary GCD under the hood
//if a+b+c > periLimit
if(a.add(b).add(c).compareTo(periLimit) > 0)break;
if(a.gcd(b).compareTo(ONE) == 0){
System.out.print(" primitive");
//if a^2 + b^2 != c^2
primCount++;
if(a.pow(2).add(b.pow(2)).compareTo(c.pow(2)) != 0)
}
continue;
System.out.println();
}
tripCount++;
}
System.out.print(a +", "+b+", "+c);
}
}
//does binary GCD under the hood
System.out.println("Up to a perimeter of " + periLimit + ", there are "
if(a.gcd(b).compareTo(BigInteger.ONE) == 0){
+ tripCount + " triples, of which " + primCount + " are primitive.");
System.out.print(" primitive");
}
primCount++;
}
System.out.println();
}
}
}
System.out.println("Up to a perimeter of "+periLimit+", there are "+
tripCount+" triples, of which "+primCount+" are primitive.");
}
}</lang>
}</lang>
Output:
Output: