' Puzzlet #040
' A man has twin sons. The product of his and
' his wife's ages, divided by the sum of his
' son's ages, is 57. He is 5 years older than
' his wife, but not yet retired.
' What are this family's respective ages?
' By Dave Ellis.
declare PrintResult()
def pa, twin: int
def result: float
pa = 22
openconsole
print "Searching ...": print
print "Pa Ma 1st Twin 2nd Twin"
do
twin = 1
do
result = pa*(pa - 5)/2/twin
if result = 57.0
PrintResult()
endif
twin = twin + 1
until pa - twin < 20
pa = pa + 1
until pa > 65
print: print "No more!"
print: print "Press a key to exit ..."
do: until inkey$ <> ""
closeconsole
end
sub PrintResult
' pretty printer
print pa, " ", pa - 5,
print using ("######", twin),
print using ("##########", twin)
return
PROGRAM NOTES
Before writing any code, we must marshal the known facts about the family's ages into a simple formula.
We spoke of the product of the father and his wife's ages, but say soon after that he is five years older than his wife. Therefore, this product can be expressed as pa*(pa - 5). We then speak of the sum of his sons' ages. However, they were identified earlier as twins. So this sum can be expressed as 2*twin. Finally, we know this is equal to 57, so the equation can now be stated as result = pa*(pa - 5)/2/twin.
Note that, while variables pa and twin are of type integer, variable result is of type float. This is to allow for the fact that the formula can generate a fractional answer. Making it an integer permits rounding to occur, which can lead to an incorrect solution.
For convenience of description, the main part of the code is repeated below with line numbers.
1
2
3
4
5
6
7
8
9
10
11
do
twin = 1
do
result = pa*(pa - 5)/2/twin
if result = 57.0
PrintResult()
endif
twin = twin + 1
until pa - twin < 21
pa = pa + 1
until pa > 65
Line 1: The main DO loop starts here.
Line 2: Initialises a twin's age at its minimum possible value, one. Theoretically, this could be zero - but we know a division by the twin's age is required, and you can't divide by zero.
Line 3: The inner DO loop starts here.
Line 4: Applies the formula which was developed above and stores the result in result. This will always depend on the current values of pa and twin. pa will have its initialised value of 22 the first time around. Why? We know the father is five years older than his wife, so she would be 17 at this time. But she has already borne twins who are at least one year old, so she would have been 16 at the time of the birth. This is the minimum age for marriage in many countries. If yours is different, please feel free to revise the code accordingly!
Line 5: Checks result for a value of 57.0.
Line 6: Prints out current values of all variables. This line is only ever accessed if Line 5 returns a TRUE value.
Line 8: Increments twin's value by 1.
Line 9: This is the end of the inner loop. If twin's value is at least 21 less than pa, the loop is executed again. 21 is used here since we've already established that the father was at least 21 when he married, so his sons must be at least 21 years younger than he is.
Line 10: Increment the value in pa by one.
Line 11: This is the end of outer loop. If pa's value is 65 or less, the loop is executed again. If it's greater he has passed retirement age, so the program halts.
When you run the code, it produces the inset screen.
There are three solutions, but you can find more if you change the minimum and maximum ages used for the father.
Searching ...
Pa Ma 1st Twin 2nd Twin
24 19 4 4
38 33 11 11
57 52 26 26
62 57 31 31
No more!
Press any key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: December 10th, 2006.