This Puzzlet was based on a bus time table originally. Without going into too much detail, a chap waiting for a bus on a dark and rainy night whiled away the time playing mental arithmetic games with the route numbers of the buses. The REMs below describe the Puzzlet he made up from them. The code which follows solves it.
' Puzzlet #008.
' 3,5,7,14,18,23,32,43,51,53,62,71,83,84,96.
' Let a Sum be the sum of any 3 numbers from
' the data set above. Many Sums can be made
' in more than one way. For example, you can
' obtain the Sum 101 in at least 2 different
' ways: 18 + 32 + 51, or 7 + 23 + 71. Note
' that each Sum must use 3 different numbers
' from the data. What Sum can be made the
' most number of times?
' By Dave Ellis.
declare LoadSumArray()
declare SortSumArray()
def datArray[16]: int
def sumArray[456]: int
def count, n, maxCount, maxTot: int
datArray = 0,3,5,7,14,18,23,32,43,51,53,62,71,83,84,96
openconsole
print "Calculating - please wait ..."
LoadSumArray()
SortSumArray()
n = 1
maxCount = 1
maxTot = 0
count = 1
do
if sumArray[n + 1] = sumArray[n]
count = count + 1
else
if count > maxCount
maxCount = count
maxTot = sumArray[n]
endif
count = 1
endif
n = n + 1
until n > 454
print: print "The sum ", maxTot, "occurs ", maxCount, "times."
print "This is the maximum number of repeats for any sum."
print: print "That's all, folks!"
print: print "Press any key to continue ..."
do: until inkey$ <> ""
closeconsole
end
sub loadSumArray
' generates all 455 possible combinations of the
' data set in datArray (that is, 3 from 15) and
' loads them into sumArray
def counter, i, j, k, sum: int
count = 0
for i = 1 to 13
for j = i + 1 to 14
for k = j + 1 to 15
counter = counter + 1
sum = datArray[i] + datArray[j] + datArray[k]
sumArray[counter] = sum
next k
next j
next i
return
sub SortSumArray
' Sorts the contents of sumArray into ascending
' order using a simple bubble sort
def endpoint, flag, i: int
do
endpoint = 454
flag = -1
for i = 1 to endpoint
if sumArray[i] > sumArray[i + 1]
flag = 0
sumArray[0] = sumArray[i]
sumArray[i] = sumArray[i + 1]
sumArray[i + 1] = sumArray[0]
endif
next i
endpoint = endpoint - 1
until flag = -1
return
PROGRAM NOTES
The program begins by creating an array, dataArray[16], to hold the numbers to be summed. IBasic numbers the cells of an n-sized array from 0 to n-1. I prefer to use cell numbers 1 to n, so I dimensioned the array with 16 cells. Once the program declarations are complete, the data is loaded into the array.
Another array is declared in the declarations: sumArray[456] , whose purpose is to hold all possible sums of any 3 data elements from the 15 we are given. The figure of 455 results from the fact that my calculator tells me 15C3 = 455.
The preamble to the main routine of the program begins by calling subroutine LoadSumArray(), whose task is to generate all possible sums of any 3 of the data items stored in datArray and then load them into sumArray.
In order to decide how many of each sum exist in sumArray, the sums need to be sorted numerically. This is also accomplished at the preamble stage, by calling subroutine SortSumArray(). This is a completely standard bubble sort routine.
Now we're in a position just to scan through sumArray in order from cell 1 to cell 456 and count the maximum number of times the same sum occurs in order. The main routine starts and carries out this task in its DO loop.
If any cell sum is equal to the next cell sum, variable count is incremented by one. When the next cell is different, the incrementing is stopped, sum's current value is stored in maxCount, and count is reset to zero ready to begin again. The actual sum that was being counted is stored in maxTot.
Eventually, all the data in sumArray will have been inspected, and the main DO loop is exited. At this point all that remains to do is print out maxTot and maxCount.
When you run the code, it produces the inset screen.
This shows that the sum 108 can be formed in 8 different ways taking 3 numbers at a time from the original data set, which is more times than any other sum available.
Calculating - please wait ...
The sum 108 occurs 8 times.
This is the maximum number of
repeats for any sum.
That's all, folks!
Press any key to continue ...
Finally, if you find you can't live without knowing just how those 8 ways arose, do the following. Make datArray [456, 5]. Use dimension 1 to store each sum, and dimensions 2, 3, and 4 to store the numbers from which the sum was calculated. When sorting sumArray, ensure dimensions 2 through 4 are always moved to stay with dimension 1. Finally, add a few lines after the main routine to have sumArray print out the data dimensions whenever it encounters a sum equal to maxSum. Voile! If you want help with this, please e-mail at the address shown on the foot of the page.
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.