Results 1 to 14 of 14

Thread: Pythagorean Triple!!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20

    Pythagorean Triple!!!

    Can someone help me with a program that shows all the possible values for a Pythagorean Triple (i.e. 3,4,5). By the way, im putting it in a list box,if that would say anything

    Thanks,

    -Keith

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    since there are an infinite number of such triples, I assume you don't REALLY mean you want them all.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    Oh yeah, sorry about that. I meant only up to 100.

    Sorry,

    -Keith

  4. #4
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155

    Brute force probably?

    Loop from i =1 to 100, and each time through check if i^2 + (i+1)^2 = (i+2)^2.

    If it does, keep it and carry on; if it doesn't, just carry on.

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    VB Code:
    1. Private Sub Command1_Click()
    2.    Dim a As Long
    3.    Dim b As Long
    4.    
    5.    For a = 1 To 100
    6.       For b = 1 To 100
    7.          List1.AddItem "a= " & a & "  " & "b= " & b & "  " & "Hyp= " & Round(Sqr(a ^ 2 + b ^ 2), 3)
    8.       Next
    9.    Next
    10. End Sub

  6. #6
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155
    Bruce, a Pythagorean triple is 3 consecutive integers which fit the equation, like 3-4-5, not just all the combinations of a & b with c rounded. 4-5-6 isn't one, because the 6 should be 6.4, and 30-31-43 is way off.

  7. #7
    New Member KingMoogle's Avatar
    Join Date
    Jan 2003
    Posts
    15
    Originally posted by Spooner
    Loop from i =1 to 100, and each time through check if i^2 + (i+1)^2 = (i+2)^2.

    If it does, keep it and carry on; if it doesn't, just carry on.
    A Pythagorean triple does not have to be x,x+1,x+2

    For example: 5, 12, 13 is a Pythagorean triple, but follows the form x,x+7,x+8. That method would not catch this one.

    This should do it, but it will produce some repeats (x,y,z & y,x,z), although it would be easy to fix this.

    VB Code:
    1. Dim i As Integer
    2. Dim j As Integer
    3. Dim k As Double
    4. For i = 1 To 100
    5.     For j = 1 To 100
    6.         k = Sqr(i ^ 2 + j ^ 2)
    7.         If k = Int(k) And k <= 100 Then
    8.             List1.AddItem CStr(i) + ", " + CStr(j) + ", " + CStr(k)
    9.         End If
    10.     Next j
    11. Next i
    Last edited by KingMoogle; Jan 29th, 2003 at 03:37 AM.

  8. #8
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155

    Thumbs up I stand corrected

    I could have sworn they had to be consecutive, but I checked up at Mathworld and you're right, they just need to be all integers.

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    A little modification of KingMoogle's code, and the double entries are gone.
    VB Code:
    1. Dim i As Integer
    2. Dim j As Integer
    3. Dim k As Double
    4. For i = 1 To 100
    5.     For j = i To 100 'changed 1 into i
    6.         k = Sqr(i ^ 2 + j ^ 2)
    7.         If k = Int(k) And k <= 100 Then
    8.             List1.AddItem CStr(i) + ", " + CStr(j) + ", " + CStr(k)
    9.         End If
    10.     Next j
    11. Next i

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here is my version.
    VB Code:
    1. Private Sub Form_Load()
    2. Dim a As Integer
    3. Dim b As Integer
    4. Dim c As Integer
    5.  
    6.     For a = 1 To 100
    7.         For b = a + 1 To 100
    8.             For c = b + 1 To 100
    9.                 If (a ^ 2) + (b ^ 2) = c ^ 2 Then
    10.                     List1.AddItem a & ", " & b & ", " & c
    11.                 End If
    12.             Next c
    13.         Next b
    14.     Next a
    15. End Sub

  11. #11
    Junior Member
    Join Date
    Jan 2003
    Posts
    21
    As all the Pythagorean triples are given by
    a=m^2-n^2
    b=2mn
    c=m^2+n^2
    such that m, n are natural numbers

    So we can use the following code to save over 99% of CPU time

    VB Code:
    1. For m=2 to 100
    2.     For n=1 to m-1
    3.         If m^2+n^2<=100^2
    4.             List1.AddItem Str(m^2-n^2) & ", " & 2*m*n & ", " & Str(m^2-n^2)
    5.         Else
    6.             Exit For
    7.         End if
    8.     Next n
    9. Next m

  12. #12
    New Member KingMoogle's Avatar
    Join Date
    Jan 2003
    Posts
    15
    pi3-14159, some of the triples that your code gives are over 100. While the method you used for getting the triples works, it's not quite as easy to ensure that you're getting all values between any particular range. (not to mention that it doesn't save that much CPU time)

  13. #13
    Junior Member
    Join Date
    Jan 2003
    Posts
    21
    Thank you very much for your comments.
    I've look at my code closely and I've found out what the problem is. It is because I am not following the original mathematical formula rigorously. It should be
    For all natural numbers k,m,n , such that m,n are coprime
    a=k(m^2-n^2)
    b=2kmn
    c=k(m^2+n^2)
    Then {a,b,c} denotes the set of all Pythagorean triples

    But in that case, the program would be much more complicated.

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by Spooner
    Bruce, a Pythagorean triple is 3 consecutive integers which fit the equation, like 3-4-5, not just all the combinations of a & b with c rounded. 4-5-6 isn't one, because the 6 should be 6.4, and 30-31-43 is way off.

    Spooner,

    I appreciate that only Int were required (consec sets) and
    I didn't alow for that.
    However, I would like to point out the code I posted would NOT
    do as you stated as I rounded to 3 decimal places. So 4-5-6.403 is
    a result, not 4-5-6!


    Bruce.

Posting Permissions

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



Click Here to Expand Forum to Full Width