|
-
Jan 29th, 2003, 01:05 AM
#1
Thread Starter
Junior Member
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
-
Jan 29th, 2003, 01:18 AM
#2
PowerPoster
since there are an infinite number of such triples, I assume you don't REALLY mean you want them all.
-
Jan 29th, 2003, 01:28 AM
#3
Thread Starter
Junior Member
Oh yeah, sorry about that. I meant only up to 100.
Sorry,
-Keith
-
Jan 29th, 2003, 01:46 AM
#4
Addicted Member
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.
-
Jan 29th, 2003, 02:16 AM
#5
VB Code:
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
For a = 1 To 100
For b = 1 To 100
List1.AddItem "a= " & a & " " & "b= " & b & " " & "Hyp= " & Round(Sqr(a ^ 2 + b ^ 2), 3)
Next
Next
End Sub
-
Jan 29th, 2003, 03:14 AM
#6
Addicted Member
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.
-
Jan 29th, 2003, 03:27 AM
#7
New Member
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:
Dim i As Integer
Dim j As Integer
Dim k As Double
For i = 1 To 100
For j = 1 To 100
k = Sqr(i ^ 2 + j ^ 2)
If k = Int(k) And k <= 100 Then
List1.AddItem CStr(i) + ", " + CStr(j) + ", " + CStr(k)
End If
Next j
Next i
Last edited by KingMoogle; Jan 29th, 2003 at 03:37 AM.
-
Jan 29th, 2003, 03:45 AM
#8
Addicted Member
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.
-
Jan 29th, 2003, 04:28 AM
#9
A little modification of KingMoogle's code, and the double entries are gone.
VB Code:
Dim i As Integer
Dim j As Integer
Dim k As Double
For i = 1 To 100
For j = i To 100 'changed 1 into i
k = Sqr(i ^ 2 + j ^ 2)
If k = Int(k) And k <= 100 Then
List1.AddItem CStr(i) + ", " + CStr(j) + ", " + CStr(k)
End If
Next j
Next i
-
Jan 29th, 2003, 05:48 AM
#10
Here is my version.
VB Code:
Private Sub Form_Load()
Dim a As Integer
Dim b As Integer
Dim c As Integer
For a = 1 To 100
For b = a + 1 To 100
For c = b + 1 To 100
If (a ^ 2) + (b ^ 2) = c ^ 2 Then
List1.AddItem a & ", " & b & ", " & c
End If
Next c
Next b
Next a
End Sub
-
Jan 29th, 2003, 11:06 AM
#11
Junior Member
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:
For m=2 to 100
For n=1 to m-1
If m^2+n^2<=100^2
List1.AddItem Str(m^2-n^2) & ", " & 2*m*n & ", " & Str(m^2-n^2)
Else
Exit For
End if
Next n
Next m
-
Jan 29th, 2003, 01:11 PM
#12
New Member
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)
-
Jan 29th, 2003, 11:19 PM
#13
Junior Member
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.
-
Jan 30th, 2003, 03:21 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|