THE PUZZLET PAGE

PUZZLET 078 - THE SOLUTION

This program can be solved by logic, as Ken Duisenberg has pointed out (see the contributions page).  However, solving it using a computer program is a worthwhile exercise in its own right, since you have to work out an appropriate algorithm and then design functions for testing that all digits in a number are unique and another for testing that all digits are identical.

' Puzzlet #078

' How  many 2-digit integer pairs are there
' whose products are 3-digit numbers having
' all digits the same? In each pair, the two
' 2-digit integers must be different.
' Example: 12 x 37 = 444.
' By Dave Ellis.

declare AllDigitsDiff(a$: string)
declare AllDigitsSame(n: int)
declare PrintResult()
def int1, int2, prod: int
def int$: string
openconsole
print "Searching ...": print

for int1 = 10 to 98
    for int2 = int1 + 1 to 99
        int$ = ltrim$(str$(int1)) + ltrim$(str$(int2))
        if AllDigsDiff(int$)
            prod = int1 * int2
            if AllDigitsSame(prod)
                PrintResult()
             endif
        endif
    next int2
next int1

print: print "Press any key to exit ..."
do: until inkey$ <> ""
closeconsole
end

sub AllDigsDiff(a$)
' if all the characters of a$ are
' unique, returns -1, else returns 0.
def flag, i1, i2, L: int
L = len(a$)
i1 = 1
flag = -1
do
    i2 = i1 + 1
    do
        if mid$(a$, i1, 1) = mid$(a$, i2, 1)
            flag = 0
        endif
        i2 = i2 + 1
    until (flag = 0) | (i2 > L)
    i1 = i1 + 1
until (flag = 0) | (i1 > L - 1)
return flag

sub
AllDigitsSame(n)
' if every digit in n is identical,
' returns -1, otherwise returns 0
def flag, i, L: int
def n$, f$: string
n$ = ltrim$(str$(n))
f$= left$(n$,1)
L = len(n$)
i = 2
flag = -1
do
    if mid$(n$,i,1) <> f$
        flag = 0
    endif
    i = i + 1
until flag = 0 | i > L
return flag

sub PrintResult
' pretty printer
print int1, "x ",
print int2, "= ",
print prod
return


   
PROGRAM NOTES


Variables int1 and int2 are used to hold the 2-digit variables we want to investigate. Using nested FOR-NEXT loops, they are made to iterate over all possible combinations.

Now, all the digits in
int1 and int2 are supposed to be unique. To test this, they're combined in a string form in variable int$ and submitted to function AllDigsdiff() as parameter.  If this returns -1 (TRUE) we can proceed. If it returns 0 (FALSE), that particular combination of int1 and int2 is skipped.

AllDigsDiff() takes int$ as its argument, held in local variable a$.  a$ is iterated over in nested DO loops, using variables i1 and  i2.

If the characters in a$ pointed by
i1 and  i2 are ever the same, variable flag (which was originally initialised to -1) is reset to 0 and the DO loops exited.  If they're never the same, the DO loops execute to the bitter end, covering every possible combination of any two characters from a$.

After all this, flag is returned to the main routine.  If it's a -1 (TRUE) that indicates all the characters in int$ were unique. If it's a 0 (FALSE), at least two of its characters were identical.

At each legal combination they are multiplied together and the product held in variable prod.

prod is submitted as the parameter to function AllDigitsSame(). If a -1 is returned (code for TRUE), the result is printed to the screen by calling subroutine PrintResult().

AllDigitsSame() converts its argument n (local variable name for prod) into a string, n$. The first character of n$ is stored in variable f$.

Variable i iterates over n$ from its second character to the last, indicated by local variable L (equal to the length of the string). If at any time f$ is equal to the character currently being examined,  variable flag, originally preset to -1, is set to 0.

Iteration continues until flag is at 0 or the whole string has been examined.  When this point is reached, flag is returned.  Clearly, a 0 (FALSE) indicates a match was found - meaning at least two of the digits are the same - and a -1 (TRUE) indicates no match was found, meaning all digits are different.


When the program is run, it generates the inset screen. There are six solutions.

Note that the products 111, 222, 333, and 999 can't be generated using the rules of this Puzzlet.


Searching ...

12 x 37 = 444
12 x 74 = 888
15 x 37 = 555
18 x 37 = 666
21 x 37 = 777
24 x 37 = 888

Press a key ...




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