A cashier accidentally reverses the Euros and cents when a customer cashes a cheque, giving him the wrong amount. The customer happily discovers he now has E22.50 more than twice the cheque's actual value! How much was the cheque actually made out for?
We need to start with a little algebra to solve this one. Working in cents, we can say that the amount the cheque is intended to be cashed for, Aintended, is:
Aintended = 100*Euros + cents
The amount the cheque was actually cashed for, Acashed, is a reversal of this, that is:
Acashed = 100*cents + Euros
The relationship between the two is:
(Acashed - 2250)/2 = Aintended
Therefore:
(100*cents + Euros - 2250)/2 = 100*Euros + cents
A little manipulation of the formula gives this:
Euros = (98*cents - 2250)/199
We can use this formula in a very simple IBasic program to find the solution to the Puzzlet.
' Puzzlet #006.
' A cashier accidentally reverses the Euros
' and cents when a customer cashes a cheque.
' The customer happily discovers he now has
' E22.50 more than twice the cheque's actual
' value! How much is the cheque actually made
' out for?
' By Dave Ellis.
def flag, cents: int
def euros: float
openconsole
flag = 0
cents = 1
do
euros = (98*cents - 2250)/199
if euros - int(euros) = 0
print "Original Amount: ",
print "E",ltrim$(str$ (euros)),".",cents
flag = -1
endif
cents = cents + 1
until flag = -1 | cents > 99
print: print "That's all, folks!"
print: print "Press any key to continue ..."
do: until inkey$ <> ""
closeconsole
end
PROGRAM NOTES
We know there must be at least one cent, because the formula would generate a negative result if the variable cents were set to zero. We also know that there must be a whole number of cents, that is, an integer value. Thus, the preamble sets cents to integer type and an initial value of 1.
The variable Euros can take on fractional values when evaluated by the formula (because we're actually working in cents), and is therefore set to type float, permitting decimal points.
The preamble also generates a variable called flag, to be used as a "done" signal in the main routine. This is set to 0 initially, indicating FALSE.
The main routine tries all possible values for variable cents in the formula derived above until one of them generates an integer value of Euros. This must be the solution, and is printed out. Variable flag is also set to -1 (TRUE) at this time.
The DO loop will keep repeating with ever-increasing values of cents until either a TRUE flag is seen, or the cents value exceeds 99. There obviously can't be more than 99 cents, so this is a trap against an error in the formula.
When you run the code, it produces the inset screen. Check: 2 x 34.92 = 69.84.
69.84 + 22.50 = 92.34
92.34 = 34.92 with integer and decimal parts interchanged!
Original Amount: E34.92
That's all, folks!
Press any key to continue ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.