' Puzzlet #070
' A square is cut from the centre of a rect-
' angle. The length of the square's side is
' exactly half the length of the rectangle's
' longest side. After cutting out the square,
' it is found that the rectangle's area has
' been reduced by exactly 38%. The sides of
' the rectangle and square are all two-digit
' integers. Find their values.
' By Dave Ellis.
declare PrintResult()
def squareSide: int
def recSide: float
openconsole
print "Searching ...": print
print " Side Lengths": print
print "Square Rectangle": print
for squareSide = 1 to 49
recSide = squareSide/0.76
if recSide = int(recSide)
PrintResult()
endif
next squareSide
print: print "No more!"
print: print "Press any key to exit... ",
do: until inkey$ <> ""
closeconsole
end
sub PrintResult
' pretty printer
print squareSide, "x ", squareSide,
print " ", recSide, "x ", 2*squareSide
return
PROGRAM NOTES
Let's begin by developing an equation covering the known facts. We can call the square's side s and the rectangular cutout's shortest side y. This is shown pictorially below, together with the appropriate formula.
You'll see in a moment how this is used in the program. First, however, we need a search range so that an actual search can begin. We know the rectangle's longest side is a 2-digit integer, so the maximum length is 99.
The square's side is half the rectangle's longest side, so its maximum value as an integer is 49. Since it's also a 2-digit integer, its minimum length must be 10. So the square's range is 10 to 49.
The code uses variable squareSide to iterate over this range. For each value it applies the formula developed above, but with more meaningful variable names: recSide = squareSide/0.76
If recSide turns out to be an integer for a particular value of squareSide, we have a solution and its printed out by the subroutine PrintResult().
The result could have been found fairly easily by trial and error once you've developed the formula. Just experiment with values of squareSide that make recSide an integer, such as 19 etc.
When you run the program, it generates the inset screen.
There are two legitimate answers, with the second being exactly double the first.
Searching ...
Side Lengths
Square Rectangle
19 x 19 25 x 38
38 x 38 50 x 76
No more!
Press a key ...
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.