THE PUZZLET PAGE

PUZZLET 013 - THE SOLUTION


' Puzzlet #013.
' Up until 1971, the British used imperial currency:
' pounds, shillings, and pence.  There were 12 pence
' to the shilling, and 20 shillings to the pound. Up
' until about 1960, there were halfpennies (2 to the
' penny).  Up until about 1950, there were farthings
' (2 to the halfpenny, 4 to the penny).
'
' A certain sum in pounds, shillings, and pence, ex-
' pressed as a five-digit integer,  contains as many
' farthings as the five-digit integer.  There are no
' halfpennies or farthings in the sum. What is it?
'
' Example:  9 pounds, 15 shillings, 11 pence. Call it
' 91511.  If it equalled 91511 farthings, it would be
' the answer, but it happens to equal 9404 farthings.
' By Dave Ellis.


def pounds, shillings, pence, farthings: int
def farthings$, test$: string
pounds = 0
openconsole
print
"Searching ... ": print

do

    shillings = 0
    do
        pence = 0
        do
            farthings = 4*pence + 48*shillings + 960*pounds
            farthings$ = ltrim$(str$(farthings))
            test$ = ltrim$(str$(pounds))
            test$ = test$ + ltrim$(str$ (shillings))
            test$ = test$ + ltrim$(str$ (pence))
            if test$ = farthings$
                print pounds, "pounds, ",
                print shillings, "shillings, ",
                print pence, "pence."
                print
                print
"This is exactly 12,128 farthings."
            endif
            pence = pence + 1

        until pence > 11
        shillings = shillings + 1
    until shillings > 19
    pounds = pounds + 1
until pounds > 105

print: print "That's all, folks!"
print: print "Press any key to close ..."
do: until inkey$ <> ""
closeconsole
end


PROGRAM NOTES

We need to determine a search range to begin with.  A few minutes with your calculator will show that a suitable value is £105, because this is equal to 105 x 960 = 100800.  This is an expression for £100 plus 8 shillings plus zero pence, and that is less than £105.  If you work it out for £104, you'll find it generates an expression for about £98.  Once the generated expression is equivalent to a smaller amount than the generator, there can be no solution, so £105 is the ceiling.

There are three nested DO loops in the code, and these are used simply to cycle through all possible values up to £105, bearing in mind that there are twenty shillings to the pound and twelve pence to the shilling.  For clarity, variables pounds, shillings, and pence are used.

For each value, the inner loop does the real work.  It calculates the equivalent value in farthings, using variable farthings, and converts this to farthing$, a string value.  Then the code converts the pounds, shillings, and pence into a string expression, test$ (for example, it would convert £13, 17 shilling, 11 pence into "131711").

farthing$ and test$ are compared.  If they are identical, we have a solution, and it's printed out.  Either way, the code continues working through the nested DO loops until the whole range up to £105 is exhausted, and then quits.

When you run the code, it produces the inset screen.
As you can see, there's only one valid solution.

The complexity of the old currency wasn't all bad - it produced a nation with above-average mental arithmetic skills, which seem to have been lost since decimalisation!

Searching ...

12 pounds, 12 shillings, 8 pence.

This is exactly 12,128 farthings.

That's all, folks!

Press any key to close ...

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

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