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
Printable View
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
since there are an infinite number of such triples, I assume you don't REALLY mean you want them all.
Oh yeah, sorry about that. I meant only up to 100.
Sorry,
-Keith
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.
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
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.
A Pythagorean triple does not have to be x,x+1,x+2Quote:
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.
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
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.
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
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
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
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)
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.
Quote:
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.