' Puzzlet #051
' The difference between a trapezium's
' parallel sides is 10 cm. The distance
' between the parallel sides is less than
' the shorter side. The area is 561 cm^2.
' All measurements are integer values.
' Note that trapezium here is given the
' British meaning. For US residents, you
' can substitute the word trapezoid.
' What are the lengths of the two parallel
' sides, and what is the distance between
' them?
' Find all possible solutions.
' By Dave Ellis.
def side, distance, area: int
openconsole
print "Searching ...": print
print "Side1 Side2 Distance"
distance = 1
do
side = distance + 1
do
area = distance*(side + 5)
if area = 561
PrintResult()
endif
side = side + 1
until area > 561
distance = distance + 1
until distance > 21
print: print "No more!"
print: print "Press any key to exit ... ",
do: until inkey$ <> ""
closeconsole
end
sub PrintResult
' pretty printer
print using ("####", side),
print using ("########", side + 10),
print using ("#########", distance)
return
PROGRAM NOTES
We need to think about ranges before we begin. We're told that all measurements are integers. The smallest integer we'll be dealing with is the distance between parallel sides, since this is specified as being shorter than the shortest parallel side. So this can be as small as just 1.
Now, the formula for the area of a trapezium is d(s1+s2)/2, where d is the distance between parallel sides, and s1 and s2 are the lengths of the parallel sides. We know that the difference between the parallel sides is 10, so we can modify the formula to d(2s1+10)/2, where s1 is the shorter parallel side. We also know that s1 is longer than d. So the minimum for s1 is d+1. Now we can further modify the formula to d(2(d+1)+10)/2. This simplifies to d2 + 6d - 561 = 0, a quadratic equation which can be solved to give d = 21 (to the nearest integer). So the maximum distance between parallel sides will be 21.
The shortest side's minimum value will always be d + 1. We don't know its maximum value, but it will be enough to say that its maximum value has been exceeded when it causes the area to exceed 561.
So the ranges will be:
Distance between
parallel sides
Short parallel
side
Long parallel
side
Minimum
1
2
short parallel
side + 11
Maximum
21
Restrict area
to 561
Restrict area
to 561
In the code, the variables side, distance, and area are used for the length of the shortest side, the distance between the parallel sides, and the area of the trapezium respectively.
Since the appropriate formula is area = d(s1 + s1 + 10)/2, it is simplified in the code as area = distance*(side + 5).
To simplify an explanation of the code, I've added line numbers to the extract below. These are the lines which do all the work.
1
2
3
4
5
6
7
8
9
10
11
12distance = 1
do
side = distance + 1
do
area = distance*(side + 5)
if area = 561
PrintResult()
endif
side = side + 1
until area > 561
distance = distance + 1
until distance > 21
Line 1: As discussed above, sets minimum distance between the parallel sides to 1.
Line 2: Opens the outer DO loop.
Line 3: Sets the minimum length of the shortest side to 1 greater than the distance between the parallel sides.
Line 4: Opens the inner DO loop.
Line 5: Calculates the area, using the formula derived above.
Line 6-8: If the calculated area equals 561, prints the result, otherwise does nothing.
Line 9: Increments the value of the shorter side ready for the next check.
Line 10: End of the inner DO loop. Quits if the area exceeds 561, since all further executions would also exceed 561.
Line 11: Increments the distance between the parallel sides ready for the next iteration.
Line 12: End of the outer DO loop. Will only permit further executions if the distance between the parallel sides hasn't exceeded 21, which is the maximum discussed above.
When you run the code, it generates the screen shown inset.
These are the only four possible solutions.
Searching ...
Side1 Side2 Distance
556 566 1
182 192 3
46 56 11
28 38 17
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.