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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about ...

Code:
Sub RightTries()
  Dim i             As Long
  Dim j             As Long
  Dim h             As Double

  For i = 2 To 100
    For j = i + 1 To i + 175
      h = Sqr(i ^ 2 + j ^ 2)
      If h = Int(h) Then
        If WorksheetFunction.Gcd(i, j, h) = 1 Then
          Cells(Rows.Count, "A").End(xlUp)(2).Resize(, 3).Value = Array(i, j, h)
        End If
      End If
    Next j
  Next i
End Sub

A​
B​
C​
1​
side1
side2
hypot
2​
3​
4​
5​
3​
5​
12​
13​
4​
7​
24​
25​
5​
8​
15​
17​
6​
9​
40​
41​
7​
11​
60​
61​
8​
12​
35​
37​
9​
13​
84​
85​
10​
15​
112​
113​
11​
16​
63​
65​
12​
17​
144​
145​
13​
19​
180​
181​
14​
20​
21​
29​
15​
20​
99​
101​
16​
24​
143​
145​
17​
28​
45​
53​
18​
28​
195​
197​
19​
33​
56​
65​
20​
36​
77​
85​
21​
39​
80​
89​
22​
44​
117​
125​
23​
48​
55​
73​
24​
51​
140​
149​
25​
52​
165​
173​
26​
57​
176​
185​
27​
60​
91​
109​
28​
60​
221​
229​
29​
65​
72​
97​
30​
84​
187​
205​
31​
85​
132​
157​
32​
88​
105​
137​
33​
95​
168​
193​
34​
96​
247​
265​
 
Upvote 0
BTW, the triangles can be more acute than the "+ 175" allows. For example, {99, 4900, 4901} is a right triangle.
 
Upvote 0
To accommodate that,

Code:
    For j = i + 1 To (i ^ 2 - 1) / 2
 
Upvote 0
thanks - a better way - but how is my code failing ? I checked for rounding and built in an allowable margin, but no change........
 
Upvote 0
You're code is spaghetti; I'm not going to attempt to unwind it.

Regarding {65, 156, 169}, it's a multiple of {5, 12, 13}
 
Upvote 0
thanks shg - I was taught quick basic 50 odd years ago, tended to always use it and find it too hard at 72 to learn the modern vba. Spaghetti is a bit harsh though, it first finds a right angled triangle then checks if it is a multiple of previous valid finds by seeing if each of the 3 sides divided by the same prime number.

I had not realised 65 156 169 was a multiple - no wonder it could not be found.
 
Upvote 0
Spaghetti is a bit harsh
It is, and I apologize.

Good flow-control constructs make code much easier to read and understand, and I commend them to your consideration. We are of an age where we can appreciate beauty in simplicity.
 
Upvote 0
shg - is this less spaghetti like for you - it runs very fast

Dim prnum(50)
For t = 1 To 25: prnum(t) = Cells(t, 10): Next t
' puts list of prime numbers into an array
For j = 3 To 1000
For k = j + 1 To 25000
' sets size parameters for the 2 non hypotenuse sides
temp = Sqr(j ^ 2 + k ^ 2)
' this is the potential hypotenuse
If temp = Int(temp) Then GoTo 100 Else GoTo 200
' if hypotenuse is not an integer then increase k by 1
100 For z = 1 To 25
140 If j / prnum(z) = Int(j / prnum(z)) And k / prnum(z) = Int(k / prnum(z)) And temp / prnum(z) = Int(temp / prnum(z)) Then GoTo 200 Else GoTo 170
' if j, k and temp all divide by the same number to give integer answers then duplication of earlier set of 3 found
170 Next z
Sum = Sum + 1: Cells(Sum, 1) = j: Cells(Sum, 2) = k: GoTo 200
' place result in output table
200 Next k
Next j
300 End Sub
 
Upvote 0
It's better :)

Does it give the results you expect?
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,690
Members
449,117
Latest member
Aaagu

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