THE PUZZLET PAGE

PUZZLET 022 - THE SOLUTION
' Puzzlet #022
' An eccentric left a bag of gold coins to be
' shared amongst his children when he died. It is
' only known that there were less than 5,000 coins.
' The first coin was to be given to his butler,
' then one-fifth of the remainder to his eldest
' son.  The butler was then to receive another
' coin and one-fifth of the remainder to the next
' eldest son.  This was to be repeated for all his
' five sons.  The remainder was to be divided
' equally among his five daughters. How many coins
' were there to start with, and how many did the
' butler, each son, and each daughter receive?
' By Dave Ellis.


def flag, i, nrCoins: int
def amount: float
openconsole
nrCoins = 4996
flag = 0

do
    amount = nrCoins
    ' reduce for the butler and 5 sons.
    for i = 1 to 5
        amount = 0.8*(amount - 1)
    next i
    ' is remainder shareable among 5 daughters?
    if amount/5 = int(amount/5)
        ' done
        flag = 1
    else
        ' go round again
        nrCoins = nrCoins - 5
    endif
until
flag

' print the answer
print "Number of Coins: ", nrCoins
print: print "Butler: 5 coins."
' work out each son's share
for i = 1 to 5
    nrCoins = 0.8*(nrCoins - 1)
    print: print "Son ", i, nrCoins/4, "coins"
next i
' work out each daughter's share
print: print "Each daughter: ", nrCoins/5, "coins"
do: until inkey$ <> ""
closeconsole
end

           
 
PROGRAM NOTES

It's instructive to begin by looking at the variables used in the program.  flag is used to indicate to the main DO loop that the search is complete.  i is used as an interator in the main program, and then later while printing out the answer.  The last integer, nrCoins, always holds the number of coins being considered at any point.


The variable amount is the only one that's not an integer - it's floating point.  You may wonder why this is so.  It's used to carry out tests on nrCoins to see if nrCoins divides exactly by 5.  If amount were an integer, it would return another integer when divided by 5 (rounding down).  By making it a float, it will also return a decimal part.  The very presence of a decimal part is enough to indicate that the original integer cannot be divided exactly by 5.  Use of amount in this way, holding a copy of nrCoins, effectively allows experiments to be done on nrCoins without changing its value.



The program begins by establishing the maximum possible number of coins that the eccentric old man left.  We know it's less than 5,000. We also know that, after deducting one coin for the butler, the remainder can be divided exactly by 5.  The greatest number that meets these criteria is 4,996, and that's where we start.

If 4,996 is the right value, it should be possible to do all the following and still leave an integer:

  1. Remove a coin for the butler and then lop off one-fifth of the remainder
  2. Repeat step 1 four more times.
  3. Divide what's left by five exactly (that is, with no remainder).

Steps 1 and 2 are carried out by the following lines:

for i = 1 to 5
    amount = 0.8*(amount - 1)
next i


At the end of these five iterations, we need to test amount to see if it can be divided exactly by five and still remain an integer.  This is taken care of by the following line:

if amount/5 = int(amount/5)

If it turns out still to be an integer we have our solution, the loop is exited, and the result printed out.  If not, the number of coins is reduced by the smallest increment that meets the rules, five, and the process repeated.


When you run the code,  it will produce the inset screen.  

There is only one solution to this Puzzlet.  Maybe it could be found by trial and error or a little more methodically using pencil and paper, but I think the computer program is more elegant.


Number of Coins: 3121

Butler: 5 coins.

Son 1 624 coins

Son 2 499 coins

Son 3 399 coins

Son 4 319 coins

Son 5 255 coins

Each daughter: 204 coins

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.