THE PUZZLET PAGE

PUZZLET 017 - THE SOLUTION

' Puzzlet #017.
' Given an unlimited supply of three weights,
' 3 kg, 11 kg, and 17 kg, find different ways
' of combining at least one of each weight to
' make a total of 100 kg exactly.  Example:
' 9 x 3 kg + 2 x 11 kg + 3 x 17 kg = 100 kg.
' By Dave Ellis.


declare printResult()
def w3, w11, w17: int
' w3 = 3 kg weight, w11 = 11 kg weight, etc.
w3 = 3: w11 = 11: w17 = 17
' All units are in kg.
def nrw3, nrw11, nrw17, total: int
' nrw3 = nr of 3 kg weights, etc.
def line$: string
line$ = string$(20, "-")

openconsole
print
line$
print "3 kg   11 kg   17 kg"
print line$

for nrw17 = 1 to 5
    for nrw11 = 1 to 7
        for nrw3 = 1 to 24
            total = nrw3*w3 + nrw11*w11 + nrw17*w17
            if total = 100
                printResult()
            endif
        next
nrw3
    next nrw11
next nrw17

print line$
print: print "No more found."
print: print "Press any key to close ..."
do: until inkey$ <> ""
closeconsole
end

sub
printResult
print using ("###", nrw3),
print using ("#######", nrw11),
print using ("########", nrw17)
return          
 


PROGRAM NOTES

The first thing here is to determine the maximum possible number of any of the three weights which can occur.  

  • nrw17 (the number of 17 kg weights) cannot exceed 5, since 5 x 17 = 85.   Even with one 11 kg and one 3 kg weight, this still only comes to 99 kg.       6 x 17 = 102, greater than the permitted 100 kg.
  • nrw11 (the number of 11 kg weights) cannot exceed 7, since 7 x 11 = 77, and with the obligatory 1 x 17 and  3 kg weights, it still only equals 97 kg.    8 x 11 = 88, plus 1 x 17 and 1 x 3 = 108, greater than the permitted 100 kg.
  • nrw3 (the number of 3 kg weights) cannot exceed 24, since 24 x 3 = 72, and with the obligatory 1 x 17  and 11 kg weights, it still comes to just 100 kg.       Obviously, if nrw3 is greater than 24, the total will exceed the permitted 100 kg.
These calculated values form the basis of the search range, and are used for three nested FOR loops:

for nrw17 = 1 to 5
    for nrw11 = 1 to 7
        for nrw3 = 1 to 24


These loops will generate a total of 5 * 7 * 24 = 840 combinations.  For each combination, the line total = nrw3*w3 + nrw11*w11 + nrw17*w17 totalises the weight into variable total.  If this is found to exactly equal 100, we have a solution and the subroutine printResult() prints it out.

Note that the number of combinations could have been reduced from 840, since some of them will be illegal.  For instance, 5 x 17 + 2 x 11 + 1 x 3 = 110, greater than the permitted 100 kg.  However, the increase in code complexity and running time saved between them don't justify weeding them out - each is investigated regardless.


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

So there are only six solutions to this particular Puzzlet, including the one discovered when setting the range in the Program Notes, and the one given in the example as part of the introduction
.
-----------------------------
3 kg   11 kg   17 kg
-----------------------------
 24          1          1
 13          4          1
   2          7          1
 11          3          2
   9          2          3
   7          1          4
-----------------------------

No more found.

Press any key to close ...

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

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