THE PUZZLET PAGE

PUZZLET 043 - THE SOLUTION


' Puzzlet #043
' A right-angled triangle with integer
' sides has a perimeter length of 840.
' How many such triangles are possible?
' By Dave Ellis.


declare PrintResult()
def base, count: int
def side: float
openconsole
print
"Searching ...": print
print
"#  base  side  hyp  peri": print
base = 1

do
    side = 840*(base - 420)/(base - 840)
    if side = int(side)
        count = count + 1
        PrintResult()
    endif
    base = base + 1
until base > side

print: print "No more!"
print: print "Press any key to exit ... ",
do: until inkey$ <> ""
closeconsole
end

sub
PrintResult
' pretty printer
print count,
print using ("####", base),
print using ("######", side),
print using ("######", sqrt(base^2 + side^2)),
print using ("######", 840)
return



PROGRAM NOTES

We need a formula here which will find, say, the side in terms of the perimeter when the base is given and the hypotenuse calculated.

Right Triangle
formulae

What about the search range for the base? Assuming the base to be the shortest  side (it has to be the base or side, and it could just as easily have been the side, so it's a purely arbitrary decision), and keeping to integers only, the base can be as small as 1.   We don't know the maximum value for the base, but, according to the convention we  have adopted,  it has to be smaller than the side, so we can use that as a maximum instead.  This gives us enough to write the program.

A DO loop is set up to run variable base's values through from 1 to the value of variable side. For each value of base, side's value is calculated from the formula
side = 840*(base - 420)/(base - 840). If this turns out to be an integer after testing with if side = int(side), we have a valid solution, and it's printed out by the subroutine PrintResult().

When you run the code, it will produce the inset screen very quickly.

There are eight solutions. altogether.

Searching ...

#  base  side  hyp  peri

1    40   399   401   840
2    56   390   394   840
3  105   360   375   840
4  120   350   370   840
5  140   336   364   840
6  168   315   357   840
7  210   280   350   840
8  240   252   348   840

No more!

Press any key to exit ...


MAIN MENU
DOWNLOAD CODE
ARCHIVES


Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.