THE PUZZLET PAGE

PUZZLET 057 - THE SOLUTION


' Puzzlet #057
' To find a 5-digit integer and an integer
' between 2 and 9 such that multiplying
' the two together produces the reversal
' of the larger integer.
' Format:  abcde * n = edcba
' By Dave Ellis.

declare ReverseInteger(n: int)
declare PrintResult(i: int, n: int, r: int)
def flag, it, num, revNum, prod: int
openconsole
print "Searching ...": print

for num = 10000 to 99999
    revNum = ReverseInteger(num)
    flag = 0
    it = 2
    do
        prod = it * num
        if prod > 99999
            flag = -1
        else
            if prod = revNum
                PrintResult(it, num, revNum)
            endif
        endif
        it = it + 1
    until flag = -1 | it = 10
next num

print: print "No More!"
print: print "Press any key ... ",
do: until inkey$ <> ""
closeconsole
end


sub ReverseInteger(n)
' returns the reverse of integer n
' example: n = 1234 returns 4321
def i, L: int
def n$, rev$: string
n$ = ltrim$(str$(n))
L = len(n$)
rev$ = ""
for i = L to 1 #-1
    rev$ = rev$ + mid$(n$, i, 1)
next i
return val(rev$)

sub PrintResult(i, n, r)
' pretty printer
print i, "* ",
print n, "= ",
print r
return


PROGRAM NOTES


An outer FOR-NEXT loop employs variable num to run through all possible 5 digit integers, that is, from 10,000 to 99,999. Each value of num is submitted as the parameter to ReverseInteger().  This function does what its name implies; it reverses whatever argument it receives and returns the reversed value to the calling routine.  The returned value is stored in variable revNum.

Variable flag is going to be used to check whether products of revNum and multiplying digits are greater than 99,999. The convention adopted is that when flag equals 0 the multiplication is ok, and when it equals -1 the multiplication creates too big a number. So we begin each investigation by setting flag equal to zero.

Variable it is going to be used as the multiplying digit for each value of revNum to be tested. It will be cycled through the values 2 to 9 inclusive, and is initially set to 2.

Once flag and it have been initialised, the program can enter its main DO loop. As each value of it is multiplied with revNum, the result is stored in variable prod.  If prod's value turns out to be greater than 99,999, flag is set to -1 to indicate further investigation won't be required.

If prod wasn't greater than 99,999, it's compared with revNum in the line if prod = revNum. If they are equal, of course, we've found a solution, since the reverse of the 5-digit integer in num multiplied by a single-digit integer has produced num itself, and this result is printed to the screen.

Within the DO loop, the rest of the single-digit values for it are tried in case some other result exists for the value in num.  The loop makes two tests before being executed again:  until flag = -1 | it = 10.  If flag is set to -1 there's no point in continuing with further values of it, since they will all make the same failure - prod  greater than 99,999.  And if it is now equal to 10, no further increments are required.  If either of these tests are met, the next value for num is fetched and processed by the outer FOR loop. This continues until num is greater than 99,999.


When you run the code, it generates the screen shown inset.  

As you can see, there are only two solutions.

Searching ...

9 * 10989 = 98901
4 * 21978 = 87912

No More!

Press any key ...


MAIN MENU
DOWNLOAD CODE
ARCHIVES


Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.