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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td="bgcolor:#F3F3F3"]
side1
[/td][td="bgcolor:#F3F3F3"]
side2
[/td][td="bgcolor:#F3F3F3"]
hypot
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]
3​
[/td][td]
4​
[/td][td]
5​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]
5​
[/td][td]
12​
[/td][td]
13​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]
7​
[/td][td]
24​
[/td][td]
25​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
5​
[/td][td]
8​
[/td][td]
15​
[/td][td]
17​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
6​
[/td][td]
9​
[/td][td]
40​
[/td][td]
41​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
7​
[/td][td]
11​
[/td][td]
60​
[/td][td]
61​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
8​
[/td][td]
12​
[/td][td]
35​
[/td][td]
37​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
9​
[/td][td]
13​
[/td][td]
84​
[/td][td]
85​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
10​
[/td][td]
15​
[/td][td]
112​
[/td][td]
113​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
11​
[/td][td]
16​
[/td][td]
63​
[/td][td]
65​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
12​
[/td][td]
17​
[/td][td]
144​
[/td][td]
145​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
13​
[/td][td]
19​
[/td][td]
180​
[/td][td]
181​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
14​
[/td][td]
20​
[/td][td]
21​
[/td][td]
29​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
15​
[/td][td]
20​
[/td][td]
99​
[/td][td]
101​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
16​
[/td][td]
24​
[/td][td]
143​
[/td][td]
145​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
17​
[/td][td]
28​
[/td][td]
45​
[/td][td]
53​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
18​
[/td][td]
28​
[/td][td]
195​
[/td][td]
197​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
19​
[/td][td]
33​
[/td][td]
56​
[/td][td]
65​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
20​
[/td][td]
36​
[/td][td]
77​
[/td][td]
85​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
21​
[/td][td]
39​
[/td][td]
80​
[/td][td]
89​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
22​
[/td][td]
44​
[/td][td]
117​
[/td][td]
125​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
23​
[/td][td]
48​
[/td][td]
55​
[/td][td]
73​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
24​
[/td][td]
51​
[/td][td]
140​
[/td][td]
149​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
25​
[/td][td]
52​
[/td][td]
165​
[/td][td]
173​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
26​
[/td][td]
57​
[/td][td]
176​
[/td][td]
185​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
27​
[/td][td]
60​
[/td][td]
91​
[/td][td]
109​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
28​
[/td][td]
60​
[/td][td]
221​
[/td][td]
229​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
29​
[/td][td]
65​
[/td][td]
72​
[/td][td]
97​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
30​
[/td][td]
84​
[/td][td]
187​
[/td][td]
205​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
31​
[/td][td]
85​
[/td][td]
132​
[/td][td]
157​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
32​
[/td][td]
88​
[/td][td]
105​
[/td][td]
137​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
33​
[/td][td]
95​
[/td][td]
168​
[/td][td]
193​
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
34​
[/td][td]
96​
[/td][td]
247​
[/td][td]
265​
[/td][/tr]
[/table]
 
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,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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