THE PUZZLET PAGE

PUZZLET 039 - THE SOLUTION

' Puzzlet #039
' Find three amounts of money such that
' their sum is $7.26 and their product is
' also $7.26.  All three amounts are
' greater than $1.00, and all three are
' different.
' By Dave Ellis.


declare PrintResult()
def amnt1, amnt2, amnt3: int
def flag, product, sum: int
product = 7260000: 'product in cents
sum = 726: 'sum in cents
amnt1 = 101: 'minimum possible value
flag = 0
openconsole
print
"Searching ...": print

do

    amnt2 = amnt1 + 1
    do
        amnt3 = sum - amnt1 - amnt2
        if amnt1 * amnt2 * amnt3 = product
            flag = 1
        else
            amnt2 = amnt2 + 1
        endif
    until
flag | amnt2 > 523
    amnt1 = amnt1 + 1
until flag | amnt1 > 523


PrintResult()
print: print "Press a key to exit ..."   
do: until inkey$ <> ""
closeconsole
end


sub PrintResult
def res1, res2, res3: float
def res1$, res2$, res3$: string
def p$, s$, t$: string
res1 = (amnt1 - 1)/100
res2 = amnt2/100
res3 = amnt3/100
res1$ = "$" + ltrim$(str$(res1))
res2$ = "$" + ltrim$(str$(res2))
res3$ = "$" + ltrim$(str$(res3))
p$ = " + "
t$ = " x "
s$ = " = $7.26"
print res1$+ p$ + res2$, + p$ + res3$ + s$
print res1$+ t$ + res2$, + t$ + res3$ + s$
return


PROGRAM NOTES

For convenience, the program works entirely in cents rather than dollars and cents.  Now, $7.26 is the product of 3 amounts involving dollars and cents.  To work in cents only, each of those amounts will have been multiplied by 100.  Thus, their product has to multiplied by 1003, or 106.   $7.26 becomes 7260000, and this is the value initialised in variable product.  

The sum of the three amounts won't need this adjustment, so the amount intialised in variable sum is simply 726.

Three other variables are created to hold the amounts we are seeking to determine:  amnt1, amnt2, and amnt3.  The solution to the Puzzlet is found when the product of these variables as well as their sum is equal to the values initialised into product and sum when both are converted back to dollars and cents.

We begin by setting amnt1 to its smallest possible value.  Since we are told that all amounts are greater than $1.00, this must be $1.01, or 101 cents in this context. The outer DO loop increments this 1 cent at a time until a solution is found.

With amnt1's value established, and bearing in mind that all amounts are different, we can try all possible values for amnt2 greater than 101.  This is accomplished by the second (nested) DO loop.

Within this inner DO loop, we can calculate amnt3's value from the other two:  
amnt3 = sum - amnt1 - amnt2.  Having values for all three amounts, we're in a position to multiply them together and see if they equal the stored value in product.  If they do, this must be the correct answer.  

What about making sure the product and sum are the same?  This has in fact already been done, effectively, since amnt3 was calculated using sum.

If the answer has been found, flag is set to 1 (TRUE).  This is detected by both DO loops, and prevents further executions.  The code just moves on to print the result and terminate.

Note the alternate exit conditions for both loops, that is, how to prevent eternal looping if no solution is found.  amnt1 can't be less than 101, and the other two amounts are different, so neither of them can be less than 102.

This means that any two amounts must total not less than 101 + 102 = 203.  726 (the sum) - 203 = 523.  So, if sum minus any of the amounts is greater than 523 it's illegal, and that particular set of search values can be abandoned by exiting the current DO loop.


When you run the code,  it produces the inset screen in just a couple of seconds.

Searching ...

$1.21 + $1.25 + $4.80 = $7.26
$1.21 x $1.25 x $4.80 = $7.26

Press any key ...

          

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

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