' Puzzlet #041
' To find two 2-digit integers whose product
' includes all of the digits contained in
' the original two integers. The product
' must consist of four different digits.
' By Dave Ellis.
declare AllDigitsDifferent(a: int)
declare Anagram(str1$: string, str2$: string)
declare PrintResult()
def int1, int2, prod: int
def digit$, int1$, int2$, prod$: string
openconsole
print "Searching ...": print
for int1 = 10 to 98
for int2 = int1 + 1 to 99
prod = int1 * int2
if prod > 999
if AllDigitsDifferent(prod)
int1$ = ltrim$(str$(int1))
int2$ = ltrim$(str$(int2))
digit$ = int1$ + int2$
prod$ = ltrim$(str$(prod))
if Anagram (digit$, prod$)
PrintResult()
endif
endif
endif
next int2
next int1
print: print "No more!"
print: print "Press a key to exit ..."
do: until inkey$ <> ""
closeconsole
end
sub AllDigitsDifferent(a)
' if all a's digits are different to each
' other, returns 1, else returns 0
def flag, i, j, L: int
a$ = ltrim$(str$(a))
L = len(a$)
i = 1
flag = 1
do
j = i + 1
do
p$ = mid$(a$, i, 1)
q$ = mid$(a$, j ,1)
if q$ = p$
flag = 0
endif
j = j + 1
until flag = 0 | j > L
i = i + 1
until flag = 0 | i > L - 1
return flag
sub Anagram(str1$, str2$)
' if str1$ is an anagram of str2$,
' returns 1, else returns 0.
def i$, j$, test$: string
def flag, i, j, L1, L2: int
test$ = str2$
L1 = len(str1$)
L2 = len(test$)
if L1 <> L2
flag = 0
else
for i = 1 to L1
i$ = mid$(str1$, i, 1)
j = 1
flag = 1
do
j$ = mid$(test$, j, 1)
if i$ = j$
replace$(test$, j, 1, "X")
flag = 0
endif
j = j + 1
until flag = 0 | j > L2
next i
flag = (test$ = string$(L1, "X"))
endif
return flag
sub PrintResult
' pretty printer
print int1$ + " x ", int2$,
print " = ", prod$
return
PROGRAM NOTES
We know that the required product contains four different digits. Since these are a numerical anagram of the digits contained in the two integers we started with, those integers must be different. Therefore, the search can be set up to exclude integer repeats, making the code run faster and more efficiently. You'll see that as we look at it in more detail below. For ease of explanation, I've given line numbers to that part of the code that actually runs the program.
1
2
3
4
5
6
7
8
9
10
11
for int1 = 10 to 98
for int2 = int1 + 1 to 99
prod = int1 * int2
if prod > 999
if AllDigitsDifferent(prod)
int1$ = ltrim$(str$(int1))
int2$ = ltrim$(str$(int2))
digit$ = int1$ + int2$
prod$ = ltrim$(str$(prod))
if Anagram (digit$, prod$)
PrintResult()
Lines 1 and 2: variables int1 and int2 are used as iterators to produce all possible combinations of 2-digit integers without duplicates.
Line 3: calculates the product of int1 and int2 and stores it in variable prod.
Line 4: tests prod's value. As long as it's greater than 999, we have a 4-digit integer and can proceed with further tests. Note that there's no need to test the upper value; 99*99 = 9801, a 4-digit integer. If the test is failed, the code abandons the particular pair of values in int1 and int2 and starts the main loop with the next pair of values.
Line 5: checks that all the digits used in prod are different to each other by calling function AllDigitsDifferent(). If they are, the code continues to line 6. If not, the code abandons the particular pair of values in int1 and int2 and starts the main loop with the next pair of values.
Lines 6 to 8: concatenates int1 and int2 into a single number, storing the result in variable digit$. Line 6 turns int1 into a string and stores it in int1$; line 7 turns int2 into a string and stores it in int2$; Line 7 joins them together.
Line 9: takes the value of prod, already obtained in line 3, and converts it into a string, storing the result in variable prod$.
Line 10: If digit$ and prod$ are anagrams of each other, we've found a solution. So these two variables are passed to function Anagram() so that this test can be performed.
Line 11: If Anagram() returns TRUE, we have a solution and it's printed out by subroutine PrintResult(). Otherwise, this line is skipped over.
The program continues in this fashion until all required 2-digit pairs have been investigated, printing out any that meet the requirement along the way.
We've encountered all the functions and subroutines used in this program in earlier Puzzlets. If you require further explanation, please e-mail me.
When you run the code, it produces the inset screen.
There are six unique solutions. Of course, if you multiply 93 x 15, you get a seventh, but this is only a repeat of 15 x 93. This is true of the other solutions as well.
Searching ...
15 x 93 = 1395
21 x 60 = 1260
21 x 87 = 1827
27 x 81 = 2187
30 x 51 = 1530
35 x 41 = 1435
No more!
Press any key ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.