Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right triangles from a hypotenuse - Redux

Status
Not open for further replies.

kwbMitel

Technical User
Oct 11, 2005
11,504
CA
Re: JonFer's Thread from Feb 18th
thread1551-1531677

The question being:
Given an integer representing the length of the hypotenuse of one or more right triangles, how can you determine the various integer sides that can be drawn for that hypotenuse?

Assuming that the integer provided for the Hypotenuse has a valid solution of 2 integer values for the other sides I would use the following method.

I have a formula that I use to calculate the difference between 2 known squares.
Difference = (A + B) X (A - B)

For the premise of this challenge to work, the difference in this case must be a perfect square. Also the Difference Squared must exceed the value of the Hypotenuse.

C[sup]2[/sup] = (A + B) X (A - B)

Case using 10 as the hypotenuse:
10+9*1 = 19 (not a perfect square)
10+8*2 = 36 (works - result 10-8-6)
10+7*3 = 51 nope
6 = Not required already solved above
10+5*5 = 75 nope
10+4*6 = 84 nope

Using this method I can quickly tell the following triplets and ignoring multiples of 3-4-5
13-12-5
17-8-5
25-24-7
26-10-24
29-20-21

Above 30 I can no longer do it quickly

I can more quickly determine a triplet when the number provided is a short side and odd. e.g.
3-4-5
5-12-13
7-24-25
9-41-40
11-61-60
13-84-85

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Typo above
17-8-15 17+8*9 = 225


*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
One more thing to add. Out of curiousity, and using my method, and an excel spreadsheet to do the math for me. I calculated the lowest numbers for a hypotnuse that have more than 1 right triangle solution. I only checked from 1-4000

Lowest with 1 = 5
Lowest with 2 = 25
Lowest with 7 = 325
Lowest with 13 = 1105

And oddly out of place:
Lowest with 5 = 1625

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Another typo - Lowest with 10 = 1625

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
I also missed typing
Lowest with 4 = 65


*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 



Lowest with 1 = 5
Lowest with 2 = 25
Lowest with 7 = 325
Lowest with 13 = 1105
My calculations yields
Lowest with 1 = 5
Lowest with 2 = 65
Lowest with 4 = 1105
I think that one of your 25's is a multiple 3-4-5 (15-20-25)
325 only has 2
1625 only has 2

My results 3 to 100
[tt]
a b c CNT
3 4 5 1
5 12 13 1
8 15 17 1
7 24 25 1
20 21 29 1
12 35 37 1
9 40 41 1
28 45 53 1
11 60 61 1
16 63 65 2
33 56 65 2
48 55 73 1
13 84 85 2
36 77 85 2
39 80 89 1
65 72 97 1
[/tt]
My code
Code:
Sub RtTri()
    Dim a As Integer, b As Integer, c As Integer, r As Long, i As Integer, bMult As Boolean
    Application.ScreenUpdating = False
    r = 2
    For c = 3 To 1625
        For a = 1 To c - 1
            For b = a To c - 1
                If c ^ 2 = a ^ 2 + b ^ 2 Then
                    bMult = False
                    For i = 2 To Application.Min(a, b, c)
                        If (c / i) ^ 2 = (a / i) ^ 2 + (b / i) ^ 2 _
                            And c Mod i = 0 And a Mod i = 0 And b Mod i = 0 Then
                            bMult = True
                            Exit For
                        End If
                    Next
                    If Not bMult Then
                        Cells(r, "A") = a
                        Cells(r, "B") = b
                        Cells(r, "C") = c
                        r = r + 1
                        Exit For
                    End If
                End If
            Next
        Next
    Next
    Application.ScreenUpdating = True
End Sub


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Cool, Skip - I'll double check

I wasn't eliminating multiples, If you were, that is likely the source of the difference. I was just looking for total quantity right triangles with X = Hypotenuse, not unique triangles.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 


65-16-63
65-25-60
65-33-56
65-39-52

325-36-323
325-80-315
325-91-312
325-125-300
325-165-280
325-195-260
325-204-253

1105-47-1104
1105-105-1100
1105-169-1092
1105-264-1073
1105-272-1071
1105-425-1020
1105-468-1001
1105-520-975
1105-561-952
1105-576-943
1105-663-884
1105-700-855
1105-744-817





*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 



[tt]
65-16-63
65-25-60 divisible by 5 as 5-12-13
65-33-56
65-39-52 divible by 13 as 3-4-5
[/tt]
Should I continue?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
And I repeat....

I wasn't eliminating multiples, If you were, that is likely the source of the difference. I was just looking for total quantity right triangles with X = Hypotenuse, not unique triangles.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 


But in any good mathematical solution, you reduce the results to the least common denominator. I believe that my solution does that.

But I can conceed that you may not want that, for what you are seeking.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
My answer is more like asking what are the factors of 60.

Using my method, I get 1,2,3,4,5,6,10,12,15,20,30,60

Using your method I would get 1,2,3,5 (all other results are themselves factored by the first 4)

So it's not a mathimatical differnence, its the nature of the question.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
The following python code details how many triangles & also list the sides fro each one

Code:
def checkPythag(h):
    triang=[]  #create empty list of valid triangles
    for a in range (1,h):
        for b in range (a,h):
            if ((a*a)+(b*b)==(h*h)):    #valid Pythag
                triang.append((h,a,b)) # add tupple containing all 3 sides
    return triang    #return list of valid triangles
#
max=1000 
for h in range (1,max+1):
#        print h
    triangles=checkPythag(h)
    numtri=len(triangles)
    if numtri>1:
        print "%s has %s Triangles" %(h,numtri)
        for triangle in triangles:
            print triangle

Pythons 'list' & 'Tupple' types seem realy suited to this task :)
 
@IPGuru: I'll have to take your word for it. I'm more a puzzle freak than a programmer.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Well the group name is "Puzzles for Programmers" :)

But I can understand the appeal of the pure mental exercise.

Personally I have only recently returned to programming (I have programmed 8 bit micro-controllers in assembly in a previous job 16 years ago) & would recommend Python to anyone wanting to learn the art.

The above program is probably not the best example of coding style,which I suppose proves Pythons claim to produce readable code.
 
I have programming experience but I am completely self taught.

My usual tool of late is Excel Macro's VBA. This is because my knowledge of Basic and Pascal is translatable to it.

I've programmed in Assembly (8085 chip), Cobol, C, DOS, and as mentioned, Basic, Pascal and VBA so I feel I meet the minimum requirements for this forum.

I'd never heard of Python before yesterday and although I've heard of Tubble I thought it was a DB format not a language.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Python is extensively used in linux for system admin tools but is also available for windows.
it is a fairly simple OOP script language but can be extended using imported modules (which can be more python, c or any other language).
is is also easy to embed into applications
For example 'PaintShopPro' uses it as its macro language.

Give it a try I think you will like it.
 
a bit like 'Operating System' when applied to Windoze
or 'Microsoft Works'

:)
 
What would you call using Bat files to execute commands and referencing multiple files to achieve a goal. They were DOS commands so I call it programming in DOS. I can't give you specifics as this was 20-25 years ago but there you go. As I said, I'm self taught.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top