' Puzzlet #181 ' The CDS (contiguous digit sum) of any ' integer is the sum of all the smaller ' integers that can be formed from it, ' using only contiguous digits. ' For example, CDS(8358) is 1393, the ' sum of 8,83,835,3,35,358,5,58,8. ' It just so happens that 8358 is thus a ' multiple of its own CDS. 8358/1393 = 6 ' exactly. ' Find all other 4-digit integers which ' are an exact multiple of their own CDS. ' By Dave Ellis. 'declarations declare Cont_Dig_Sum(c: int) declare PrintResult() def digSum, nmbr: int ' setup color 11,0: cls print "Searching ...": print print "Number CDS Multiple" print color 14, 0 ' main routine for nmbr = 1000 to 9999 digSum = Cont_Dig_Sum(nmbr) if nmbr % digSum = 0: 'exact multiple? PrintResult() endif next nmbr ' finished color 11, 0 print: print "No more!" print: print "Press any key to exit ... ", do: until inkey$ <> "" closeconsole end sub Cont_Dig_Sum(c) ' Returns the CDS (contiguous digit sum) ' of argument c, that is, the sum of all ' the smaller integers that can be formed ' from it, using only contiguous digits. def i, loop, temp, sum: int loop = log10(c): 'get loop size sum = 0: 'initialise sum to zero for i = 1 to loop temp = c: 'copy argument do sum = sum + temp % 10^i: 'new sum temp = temp/10: 'chop off temp LSD until temp < 10^(i - 1) next i return sum sub PrintResult ' pretty printer print " ",nmbr, print using ("######", Cont_Dig_Sum(nmbr)), print using ("######", nmbr/Cont_Dig_Sum(nmbr)) return