THE PUZZLET PAGE

PUZZLET 027 - THE SOLUTION
' Puzzlet #027
' An integer is incremented by any digit from 1
' to 9.  The new value is exactly divisible by
' the added digit.  What is the lowest value
' integer for which this is true?
' By Dave Ellis.


declare PrintResults(p: int)
def flag, i, j, k: int
def n: float
' i holds the integer to be divided
' j holds the dividing integer (1 to 9)
' k holds the sum of i + j
' n holds the result of k/j


i = 1
openconsole
print
"Calculcating ...": print

do

    k = i
    flag = 1
    j = 1
    do
        k = i + j
        n = k/j
        if n <> int(n)
            flag = 0
        else
            j = j + 1
        endif
    unti
l flag = 0 | j > 9
    i = i + 1
until flag

PrintResults(i - 1)
print: print "That's all!"
print: print "Press a key to exit ... ",
do: until inkey$ <> ""
closeconsole
end

sub
PrintResults(p)
' pretty printer
def it, sum: int
for it = 1 to 9
    sum = p + it
    print p, "+ ", it, "= ", sum,
    print " :  ", sum, "/ ", it, "= ",
    print using ("####", sum/it)
next it
return

 
PROGRAM NOTES

Variable i holds the integer we are looking for - the answer. Variable j is only used to hold the values 0 through 9. Variable k always holds the sum of i and j in the main program loop.  These three variables are all integers.

Variable n is used to hold the value of k divided by j.  This may not always be an exact division, so n is a float (floating point number).

The way these variables are employed by the program's algorithm is demonstrated by the inset flowchart.



flowchart


The current vlaue of i is loaded into k for processing without altering i's value. Variable j is set to 1 and added to k.  k is divided by j and the result loaded into n.  If this is an exact division, j is incremented and the process repeated until j has been used for all values over the range 1 to 9.

If at any point in this loop an exact division fails to occur, the loop is exited, i incremented, and the process started again with the new value of i.  This will go on until eventually a value for i is found which always produces exact divisions for all values of j from 1 through 9.  this value is in fact the solution.


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

This is the lowest-value. There are other solutions of greater value, but the code is designed to stop looking for them once the first solution has been found.


Calculating ...

2520 + 1 = 2521  :  2521 / 1 = 2521
2520 + 2 = 2522  :  2522 / 2 = 1261
2520 + 3 = 2523  :  2523 / 3 =  841
2520 + 4 = 2524  :  2524 / 4 =  631
2520 + 5 = 2525  :  2525 / 5 =  505
2520 + 6 = 2526  :  2526 / 6 =  421
2520 + 7 = 2527  :  2527 / 7 =  361
2520 + 8 = 2528  :  2528 / 8 =  316
2520 + 9 = 2529  :  2529 / 9 =  281

That's all!

Press a key to exit ...

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: October 10th, 2009.