finding a special pythagorean triplet using Euclid's formula

as the title says i'm trying to find a phythagorean triplet that has the sum 1000 (a + b + c = 1000) i wanted to do this using Euclids formula but I keep failingto find the correct values for abc so please help :D

Code:

`Euclid's formula[1] is a fundamental formula for generating Pythagorean triples given an arbitrary pair of positive integers m and n with m > n. The formula states that the integers`

a = m^2 - n ^2 , b = 2mn , c = m^2 + n ^2

form a Pythagorean triple. The triple generated by Euclid's formula is primitive if and only if m and n are coprime and m − n is odd.

If both m and n are odd, then a, b, and c will be even, and so the triple will not be primitive; however, dividing a, b, and c by 2 will yield a primitive triple if m and n are coprime.

Every primitive triple (possibly after exchanging a and b) arises from a unique pair of coprime numbers m, n, one of which is even. It follows that there are infinitely many primitive Pythagorean triples.

Code:

`import static java.lang.Math.*;`

public class Nine {

public static void main(String[] arg){

int m = 1,n = 1,r = 1,tempm,tempn,checkifcoprimes,check;

double a = 0,b = 0,c = 0,answer = 0;

while(answer != 1000){

m++;

if(m == 1000){

m = 1;

n++;

}

if(n == 1000 && answer != 1000){

System.out.println("fail");

System.exit(0);

}

tempm = m;

tempn = n;

while(r != 0){

r = m % n;

m = n;

if(r != 0)

n = r;

}

checkifcoprimes = n;

m = tempm;

n = tempn;

r = 1;

check = m-n;

if(checkifcoprimes == 1 && m > n)

{

if(m%2 != 0 && n != 0){

a = (pow(m,2) - pow(n,2)) / 2;

b = (2 * m * n) / 2;

c = (pow(m,2) + pow(n,2)) / 2;

}

else if(check%2 != 0){

a = (pow(m,2) - pow(n,2));

b = (2 * m * n);

c = (pow(m,2) + pow(n,2));

}

if(a < b && b < c){

answer = a+b+c;

if(answer == 1000)

System.out.println("found the triplets");

}

}

}

}

}

Re: finding a special pythagorean triplet using Euclid's formula

It's just a bit of simple math: if a+b+c == 1000 then m^2-n^2 + 2mn + m^2+n^2 == 2m^2 +2mn == 1000, so m(m+n) == 500. Check all divisors of 500 and you're done with it.

kind regards,

Jos

Re: finding a special pythagorean triplet using Euclid's formula

Quote:

Originally Posted by

**JosAH** It's just a bit of simple math: if a+b+c == 1000 then m^2-n^2 + 2mn + m^2+n^2 == 2m^2 +2mn == 1000, so m(m+n) == 500. Check all divisors of 500 and you're done with it.

kind regards,

Jos

but that would indeed ruin the whole purpose of doing it with Eucild's formula :P

Re: finding a special pythagorean triplet using Euclid's formula

Quote:

Originally Posted by

**Sulfuri** but that would indeed ruin the whole purpose of doing it with Eucild's formula :P

Ruin it? My method finds an m and an n and Euclid's formula finds a, b and c for you ... why do more work (especially when it isn't necessary)?

kind regards,

Jos

Re: finding a special pythagorean triplet using Euclid's formula

On the contrary. The problem is much harder and more of a trial and error situation just using sides a, b, and c. Using Euclid's formula allows a, b, and c to be expressed in terms of m and n and then reduced to a simpler formula.

Regards,

Jim

Re: finding a special pythagorean triplet using Euclid's formula

Ok, I think I found out what I was trying to say earlier. By definition, a Pythagorean primitive triplet's sides have no common factor. However, unless I am misunderstanding something, you can't find a primitive triple whose sides add up to 1000. But you can reduce Jos' equation by using number theory. Since m(m+n) = 500 and m and n are relatively prime (and one of m,n must be odd, the other even), then m + n must be odd. Since m(m+n) = 500 and m+n is odd, then m must be even because both sides of the equation are divisible by 2. So let m = 2r. r(2r + n) = 250. = 2 * 5 * 5 * 5. Since n is odd 2r + n must be odd so r is divisible by 2. so r = 2s and s(4s + n) = 125. This leads to a trivial solution where m=4s, and s(4s+n) = 5*5*5. (My apologies if this deviates too much from the forum).

Regards,

Jim

Re: finding a special pythagorean triplet using Euclid's formula

Quote:

Originally Posted by

**jim829** \(My apologies if this deviates too much from the forum).

No need to apologize at all; nowadays programming lacks sometimes obvious math and the most cripple solutions are stamped as final solutions, while a bit of math could've resulted in a much more efficient algorithm; but mathematics seem to be a dirty knowledge, it's better to hack yourself through a problem without a bit of thinking ...

kind regards,

Jos

Re: finding a special pythagorean triplet using Euclid's formula

Quote:

Originally Posted by

**JosAH** ... it's better to hack yourself through a problem without a bit of thinking ...

Stop stalking me!

:s:

Re: finding a special pythagorean triplet using Euclid's formula

Quote:

Originally Posted by

**jim829** Ok, I think I found out what I was trying to say earlier. By definition, a Pythagorean primitive triplet's sides have no common factor. However, unless I am misunderstanding something, you can't find a primitive triple whose sides add up to 1000. But you can reduce Jos' equation by using number theory. Since m(m+n) = 500 and m and n are relatively prime (and one of m,n must be odd, the other even), then m + n must be odd. Since m(m+n) = 500 and m+n is odd, then m must be even because both sides of the equation are divisible by 2. So let m = 2r. r(2r + n) = 250. = 2 * 5 * 5 * 5. Since n is odd 2r + n must be odd so r is divisible by 2. so r = 2s and s(4s + n) = 125. This leads to a trivial solution where m=4s, and s(4s+n) = 5*5*5. (My apologies if this deviates too much from the forum).

Regards,

Jim

so you're saying i can't use Euclid's formula since it only finds primitive triplets?( totally missed that when first reading about it :<)

Re: finding a special pythagorean triplet using Euclid's formula

Quote:

Originally Posted by

**Sulfuri** so you're saying i can't use Euclid's formula since it only finds primitive triplets?( totally missed that when first reading about it :<)

No, we're saying that you better use Euclid's formula because you can find the wanted a, b and c without blindly wading through the entire search space and stumbling upon the solution by accident.

kind regards,

Jos

Re: finding a special pythagorean triplet using Euclid's formula

No I am not saying that at all. Sorry if I misled you. You can find any Pythagorean triplet with Euclids formula just by making certain that m > n (so that m^2 - n^2) is > 0.

However, unless, m and n are relatively prime, and one is odd and the other even (i.e. m - n is odd), then you will not have a primitive triplet. And just to ensure we are on the same page, a primitive triplet is one where no two sides have a common divisor > 1.

Unless I made a mistake above, there is no Pythagorean primitive triplet where a, b, and c add up to 1000. However, there is a solution where they add up to 1000 for a non-primitive triplet (i.e all sides will be divisible by 5).

Regards,

Jim