THE PUZZLET PAGE

PUZZLET 064 - THE SOLUTION


The most important thing here is to establish up front what the approximate ranges for the base and side of the triangle are, so that a search can be made.

Call the triangle's three sides b, h, and s for base, hypotenuse, and side. Arbitrarily, let b be less than s. From the information given, we can say:

b + s + h + ½b.s = 280

Using pythagorus, we can substitute for h:

b + s + sqrt(b2 + s2) + ½b.s = 280

Now, the minimum size for b is 1, since we are told it's an integer. Don't forget that s will be a maximum in these circumstances. Substituting 1 for b, we have:

1 + s + sqrt(1 + s2) + ½s = 280

That 1 is comparitively trivial, and can be discarded without serious loss of accuracy, giving:

s + sqrt(s2) + ½s = 280

This can be solved easily to give s = 112.

b is a minimum for this value.  As b increases, s must decrease to give the same product.  Since s is always greater than b, b's maximum value will be s/2 - 1 = 112/2 -1 = 55.

We now have a minimum and maximum for b (1 and 55), and a minimum and maximum for s (b + 1 and 112). This is sufficient to write the code the following code. A more detailed description and explanation of the code follow it.

' Puzzlet #064

' A right-angled triangle has integer sides.
' The sum of its perimeter and area is 280.
' Find the lengths of its sides.
' By Dave Ellis

declare PrintResult(s: int, b: int, h: int)
def side, base, peri: int
def hyp: float
openconsole
print "Searching ...": print

for base = 1 to 55
  for side = base + 1 to 112
    hyp = sqrt(base^2 + side^2)
    if hyp = int(hyp)
      peri = side + base + hyp
      if (side*base)/2 + peri = 280
        PrintResult(side, base, hyp)
      endif
    endif
  next side
next base

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


sub PrintResult (s, b, h)
' pretty printer
print "Area: ", 0.5 * s * b
print "Side: ", s
print "Base: ", b
print "Hypotenuse: ", h
print "Perimeter: ", s + b + h
return


PROGRAM NOTES


Two nested FOR loops run the base and side through the ranges already discussed above, using variables base and side. For each combination of these values the hypotenuse is calculated from them, using the expression
hyp = sqrt(base^2 + side^2), and stored in variable hyp.

We know that hyp should be an integer, so it's tested now using the expression
if hyp = int(hyp). For values that prove to be integers, we now have enough information to calculate the perimeter, using the expression peri = side + base + hyp, and store it in variable peri.

All that's needed now is to calculate the area, add it to peri, and check whether their sum is 280. The expression
if (side*base)/2 + peri = 280
accomplishes this. When the expression returns TRUE, the result is printed out using subroutine PrintResult().

When you run the code, it generates the screen shown inset. 

The perimeter is 70, the area 210, and their sum 280 as required.

Searching ...

Area: 210
Side: 21
Base: 20
Hypotenuse: 29
Perimeter: 70

No more!

Press a key ...


MAIN MENU
DOWNLOAD CODE
ARCHIVES


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