' Puzzlet #224. ' ProSum Numbers are obtained when the product ' of an integer's digits divided by the sum of ' its digits is itself an integer. If the ' resultant integer can be used to produce yet ' another integer in the same way, a sequence ' is formed. It is terminated when a new term ' is less than 10. ' The first term must be at least 10, or an ' endless loop is formed. ' Find the lowest starting term for a sequence ' of two ProSum Numbers, then do the same for ' three terms and four terms. ' By Dave Ellis. ' Declarations declare ProductDigits(p: int) declare SumDigits(s: int) declare PrintResult() def count, flag, n, prod, sum, target, term: int def quotient: double ' Screen Setup color 11, 0: cls openconsole print "1st Term Nr Of Terms Last Term" color 14, 0 print ' Initialisations n = 10 target = 0 ' Main routine do term = n: 'save n count = 0: 'initialise count flag = -1: 'set flag to TRUE do prod = ProductDigits(term): 'get product sum = Sumdigits(term): 'get sum quotient = prod/sum: 'calculate quotient if quotient = int(quotient): 'is it an integer? count = count + 1: 'yes - increment tally term = quotient: 'yes - get next term else: 'no flag = 0: 'set flag to FALSE endif until (flag = 0) | (term < 10) if count > target: 'target reached? PrintResult(): 'yes - print result target = target + 1: 'yes - get next target endif n = n + 1: 'increment sequence start until target = 3 ' finished color 11, 0 print: print "That's all, Folks!" print: print "Press any key to exit ... ", do: until inkey$ <> "" closeconsole end ' +++ Functions and Subroutines +++ sub ProductDigits(p) ' Returns the product of the ' digits of integer p. Will ' give incorrect answer if p ' larger than defined limits ' for integers. ' By Dave Ellis. def tot: int tot = 1 do ' separate out LSD and totalise tot = tot * (p % 10) ' chop off LSD p = p/10 until p = 0: ' all chopped off! return tot sub SumDigits(s) ' Returns the sum of the ' digits of integer s. Will ' give incorrect answer if s ' larger than defined limits ' for integers. ' By Dave Ellis. def tot: int tot = 0 do ' separate out LSD and totalise tot = tot + (s % 10) ' chop off LSD s = s/10 until s = 0: 'all chopped off! return tot sub PrintResult() print using ("#######", n), print using ("##########", count + 1), print using ("#############", term) return