' Puzzlet #065
' Multiplying the digits of an integer together
' always produces a smaller number. If the new
' integer is submitted to the same process, an
' even smaller number will appear. Eventually,
' the resultant integer will be less than 10,
' and no further reduction is possible.
' Example: 29 -> 2 x 9 = 18 -> 1 x 8 = 8. This
' represents 2 iterations.
' What is the smallest integer that generates a
' single digit using this process within two
' iterations? 3, 4, 5, 6, and 7 iterations?
' By Dave Ellis.
declare CountIterations (c: int)
declare PrintResult(i: int, n: int)
def done, it, num: int
openconsole
print "Searching ...": print
print "Iterations Start Integer"
num = 10
for it = 2 to 7
done = 0
do
if it = CountIterations(num)
done = -1
PrintResult(it, num)
else
num = num + 1
endif
until done
next it
print: print "Press a key to exit... ",
do: until inkey$ <> ""
closeconsole
end
sub CountIterations(c)
' returns the number of iterations taken for
' parameter c to produce a result less than
' 10 using the process described above.
def count, flag, i, L, prod: int
def c$: string
count = 0
flag = 0
c$ = ltrim$(str$(c))
do
count = count + 1
L = len(c$)
prod = val(left$(c$,1))
for i = 2 to L
prod = prod * val(mid$(c$,i,1))
next i
if prod < 10
flag = -1
else
c$ = ltrim$(str$(prod))
endif
until flag
return count
sub PrintResult(i,n)
' pretty printer
print using ("######", i),
print space$(11),
print using ("#####", n)
return
PROGRAM NOTES
Variable it is used to count the number of iterations required to terminate the series as described in the REMs. This is accomplished by also making it the iterator of a FOR loop.
The starting number for each sequence is held in variable num, and its lowest possible value is 10 - the lowest integer with more than one digit. num is always set to 10 at the start of each iteration of the FOR loop.
Within each FOR iteration, a DO loop is started to process num. It's submitted as the parameter to function CountIterations(), whose job is to count the number of iterations necessary to reduce its argument to a single digit using the process described in the REMs. If the returned value is identical to the current value if it, we have a solution, and it's printed out using subroutine PrintResult(). Variable done is set to -1 (TRUE). If it wasn't identical to it, num is incremented.
At the end of the DO loop, done is examined. If it's still at its preset value of 0 (FALSE), the DO loop is executed again, this time with the incremented value of num. The process repeats until eventually a solution is found.
At this point, the FOR loop runs again with the next value of it, and so on until it has incremented past 7, its maximum required value.
CountIterations() relies on string-slicing to work. It isolates one digit at a time of its argument, c (local variable name for num), by converting c into a string, c$.
The individual digits are multiplied together one at a time, the running total being stored in variable prod. When all digits have been processed in this way, a check is made to see if its value is less than 10. If it is, we have a solution. If not, the process is repeated again, using the new value of prod instead of the original argument. The number of times it has to be repeated is stored in variable count.
It's the actual value of count that we need, and when prodcount's value eventually falls below 10 it's that is returned to the calling routine.
When you run the code, it generates the screen shown inset.
It looks as if a pattern develops for the larger numbers of iterations. Maybe you can find a formula ...
Searching ...
Iterations Start Integer
2 25
3 39
4 77
5 679
6 6788
7 68889
Press any key to close ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.