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