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.
 
A much faster solution than my 1st go
Code:
def checkPythag(max):
    triang=[]  #create empty list of valid triangles
    for a in range (1,max):
        for b in range (a,max):
            h=float((a*a)+(b*b))**.5
            if (float(int(h))==h):
                triang.append((h,a,b))
    return sorted(triang, key = lambda a: a[0])    #return list of valid triangles
#
max=1000
triangles=checkPythag(max)
#print "total number of pythag triangles %s" % len(triangles)
temp=0
while(temp<len(triangles)-1):
    count=1
    while(1):
        if triangles[temp][0]!=triangles[temp+count][0]:
            break
        count +=1
    if count>1:print triangles[temp][0],count
    temp=temp+count

This creates a list of all matching triangles sorted by Hypotenuse then scans the list to print any with more than 1 hit

a scan 1-1000 takes 4 seconds compared to 206
a scan 1-5000 takes 100 seconds & I am not even going to try V1!
it is easy to add the results into a list & sort by number of triangles
the top ten in the 1-5000 all have 13 hits
1105
1885
2210
2405
2465
2665
3145
3315
3445
 
>What would you call using Bat files


I was actually being a bit tongue-in-cheek with my comment...

However, if pressed, I'd call it batch file or shell programming.

Note that many of the batch files that you wrote all those years ago would probably run quite happily on Windows 7, which does not have any DOS at all - so those batch files clearly cannot be DOS programs ...
 
@ Strongm - I think I was in a bad mood yesterday and I was taking teasing as ridicule. No worries. Nothing I've ever programmed amounted to much anyway. Tax programs, Ascii Art, Lottery Number generators and the like.

@ IPGuru - your list matches mine, when counting all possible triangles with Hypotnuse X. I was kindof wondering about the 1 or 2 that result in 10. They seem out of place. Another interesting thing (to me anyway). There is a pattern to some of the short and long sides. (possibly all). Some of the patterns may be just too large for me to recognise.

*******************************************************
Occam's Razor - All things being equal, the simplest solution is the right one.
 
Ascii Art, No that is an amazing skill
I assume the Lottery number predictor didn't work though

Seriously I wasn't teasing& considering its features (or lack of) anyone who can get a MSdos batch program to do anything more complicated that starting a program is doing pretty good.

Windoze does still contains a variant of MSDos it is just hidden & has been given a fresh coat of paint!
 
IpGuru - Actually, the lottery number generators were designed to prove a formula I developed for calculating the probability of matching M numbers of C choices out of N Number os balls. It works like a charm actually. (Both formula and Program) Sorry I was so defensive otherwise.

*******************************************************
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