`
THE PUZZLET PAGE

PUZZLET 076 - THE SOLUTION


One way to solve this program is to use Hero's Rule for the area of a triangle:

Formula 1
Triangle

We are told that two of the sides of the larger triangle are 15 and 18.  Call the unknown side c1.  Then the s value is:

Formula 2

When you substitute the s, a, b, and c values in the formula, everything works out very neatly. There are four terms inside the square root, and they pair off twice into the algebraic difference of two squares,  so that you get:

 Formula 3

This can be simply expressed as:

Formula 5

Now we have enough to write some code.


' Puzzlet #076

' Two  triangles both have at least  one
' side of length 15.  The ratio of their
' areas is 9:7. All sides and both areas
' are integers.  One of the sides of the
' larger triangle is 18.   Every side in
' both triangles lies between 10 and 99.
' Find the area and side lengths of each
' triangle.
' By Dave Ellis.

declare SolveTriangle2(area: float)
declare PrintResult(side2b: int, side2c: int)
def side1c, temp: int
def area1, area2: float

openconsole
print "Searching ...": print

for side1c = 10 to 99: 'unknown side triangle 1
    temp = 1098*side1c^2 - side1c^4 - 9801
    if temp > 0: 'prevents taking sqrt of neg nums
        area1 = 0.25*sqrt(temp)
        area2 = 7 * area1/9
        if area2 = int(area2)
            SolveTriangle2(area2)
        endif
    endif
next side1c

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

sub
SolveTriangle2 (area)
' takes 7/9 the area of triangle 1 and tries to
' generate another triangle, with side 1 = 15,
' sides 2 and 3 between 10 and 99, having the
' same area.
def side2b, side2c: int
def result: float
for side2b = 10 to 98
    for side2c = side2b + 1 to 99
        result = (side2b + side2c)^2 - 225
        result = result * (15 - side2b + side2c)
        result = result * (15 + side2b - side2c)
        if result = 16 * area * area
            PrintResult(side2b, side2c)
        endif
    next side2c
next side2b
return

sub PrintResult (side2b, side2c)
' pretty printer
print "Triangle 1."
print: print "Area:  ", area1
print "Side 1:  15"
print "Side 2:  18"
print "Side 3:  ", side1c
print: print "Triangle 2."
print: print "Area:  ", area2
print "Side 1:  15"
print "Side 2:  ", side2b
print "Side 3:  ", side2c
print: print "Ratios of areas:  "
print: print area1,"/ ", area2, " = ", "9/7"
print
return
   
   
PROGRAM NOTES

Variable side1c is used to represent the unknown side of the larger triangle. The other two sides are given in the problem statement as 15 and 18. We know that side1c's value lies somewhere between 10 and 99, so the first line of the program iterates over that range.

Variable temp is used as a temporary storage place for trial values of the area. Every trial value is obtained using the formula derived above, applied by the line:
temp = 1098*side1c^2 - side1c^4 - 9801

This value is actually equal to 16 times the square of the area, so it will be necessary later to take its square root. It is in fact possible for the trial value to be negative over the range, so taking its square root won't always work. The line if temp > 0 is used to protect against this error.

Having got this far, we can calculate the area of the larger triangle and store it in variable area1. Since the trial value in temp is equal to 16 times the area, we need to take a quarter of temp's square root. Line
area1 = 0.25*sqrt(temp) does the job.

We know the smaller triangle's area is 7/9 of the larger. This is now calculated and stored in variable area2., using line
area2 = 7 * area1/9

Both triangles' areas are supposed to be integers. Because of the way area2 was derived we can be sure that, if area2 is integer, so is area1. If this is the case, we have a potential solution.

At this point, the only information missing are two of the smaller triangle's sides (we were told in the problem statement that one of the sides is 15). The main routine goes on to call the subroutine SolveTriangle2() to find the other two sides.

SolveTriangle2() declares the variables side2b and side2c to search for the other two sides. They are iterated over all possible legal values for the wanted side lengths. Hero's Rule has been used again to generate the formulae you see used in this subroutine.

Now, the argument for
SolveTriangle2() was the area as derived from the larger triangle. If SolveTriangle2() comes up with the same area, we have a valid solution, and it's printed out directly from within the subroutine. PrintResult() is called for this purpose.

When the program is run, it generates the inset screen. There is only one solution.

The values in the answer are very simple, but the math to get there is relatively complicated!


Triangle 1.

Area:  108
Side 1:  15
Side 2:  18
Side 3:  15

Triangle 2.

Area:  84
Side 1:  15
Side 2:  13
Side 3:  14

Ratios of areas:

108 / 84  = 9/7




Site design/maintenance: Dave Ellis E-mail me!
Last Updated: October 10th, 2009.