right angle triangle calculator - problems

oldbrewer

Well-known Member
Joined
Apr 11, 2010
Messages
11,012
this code finds 2 non hypotenuse sides that form a right angle triangle then checks if it is a multiple of a previous "find". it finds nothing after 65 72 97 ie it does not find 65 156 169.

J column is a list of prime numbers below 100

Sub Macro4()
'
' Macro4 Macro
' Macro recorded 13/08/2017 by bob
'


'
For j = 3 To 69
Cells(1, 8) = j
For k = j + 1 To 175
temp = j ^ 2 + k ^ 2: Cells(1, 4) = temp
temp1 = Sqr(temp): Cells(1, 5) = temp1
temp2 = Int(temp1): Cells(1, 6) = temp2
If temp2 = temp1 Then GoTo 100 Else GoTo 200
100 For z = 1 To 25
If Cells(z, 10) > k Then GoTo 150 Else GoTo 140
140 If j / Cells(z, 10) = Int(j / Cells(z, 10)) Then GoTo 142 Else GoTo 170
142 If k / Cells(z, 10) = Int(k / Cells(z, 10)) Then GoTo 144 Else GoTo 170
144 If temp / Cells(z, 10) = Int(temp / Cells(z, 10)) Then GoTo 200 Else GoTo 150
150 Sum = Sum + 1: Cells(Sum, 1) = j: Cells(Sum, 2) = k: GoTo 200
170 Next z
200 Next k
Next j
End Sub
 
Just to put my 2 cents in . . .

There is a mathematical theory that says given any 2 integers i and j, where j > i, you can create a Pythagorean triple with these formulas:

a = j^2 - i^2
b = 2 * i * j
c = j^2 + i^2

That part is easy enough to prove algebraically. It's a little tougher to prove that you'll find every possible Pythagorean triple that way, but trust me, that's true too. Given that, it's easy to write a program to find them all:

Code:
Sub Triples()
Dim i As Long, j As Long, a As Long, b As Long, c As Long, Sum As Long

    Sum = 0
    For i = 1 To 100
        For j = i + 1 To 101
            a = j ^ 2 - i ^ 2
            b = 2 * i * j
            c = j ^ 2 + i ^ 2
            If WorksheetFunction.Gcd(a, b, c) = 1 Then
                Sum = Sum + 1
                Cells(Sum, "A") = a
                Cells(Sum, "B") = b
                Cells(Sum, "C") = c
            End If
        Next j
    Next i
            
End Sub
A few things of interest. A triple that is not a multiple of another triple is called a primitive.

Next, this algorithm sometimes creates numbers where a > b. If that's of concern, you can add a check for that and switch them. Finally, the triples aren't really in order. It would probably look better if they were sorted by column A, then column B.

Anyway, I put a lot of study into this kind of thing when I was younger, I hope there's something of interest in it! :)
 
  • Like
Reactions: shg
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
shg - yes it does - but it is bugging me why the original code never found ANYTHING beyond 65 72 97 - I will not give up and will insert artificial break points and print out intermediate values to solve it.....
 
Upvote 0
Eric - thank you

I have found every unique right angle triangle up to sides of around 15000 and am running into precision problems - thinking about trying to make a long multiplication spreadsheet - I seem to think there is a long division way of getting square roots - do you know of it ?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top