Results 1 to 11 of 11
  1. #1
    Sulfuri is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default 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.
    Java 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");
    		
    		 
    	}
       }
      }
     }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Sulfuri is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Sulfuri View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default 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
    Last edited by jim829; 03-07-2013 at 05:54 PM. Reason: bad morning
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by jim829 View Post
    \(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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    Quote Originally Posted by JosAH View Post
    ... it's better to hack yourself through a problem without a bit of thinking ...
    Stop stalking me!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Sulfuri is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by jim829 View Post
    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 :<)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Sulfuri View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Kwadratic formula
    By BURNY26 in forum New To Java
    Replies: 20
    Last Post: 03-26-2012, 05:31 PM
  2. Help: Pythagorean Triples
    By mchahal22 in forum New To Java
    Replies: 18
    Last Post: 12-26-2010, 08:49 AM
  3. changing TF formula
    By o.imen in forum Lucene
    Replies: 0
    Last Post: 09-16-2010, 02:20 PM
  4. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 07:57 PM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 11:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •