' Puzzlet #014.
' Multiplying three different 2-digit integers
' together generates a 6-digit integer which
' contains all the digits of the original numbers.
' Note that the 6-digit integer must have unique
' digits.
' Example: 46 x 72 x 89 = 294768.
' Are there any other examples like this?
' By Dave Ellis.
declare noRepeats(n$: string)
declare anagram(t$: string, p$: string )
def it1, it2, it3, prod: int
def it1$, it2$, it3$, prod$, test$: string
openconsole
print "Checking ..."
for it1 = 10 to 97
it1$ = ltrim$( str$(it1))
for it2 = it1 + 1 to 98
it2$ = ltrim$ (str$(it2))
for it3 = it2 + 1 to 99
prod = it1 * it2 * it3
if prod > 99999
it3$ = ltrim$(str$ (it3))
test$ = it1$ + it2$ + it3$
if noRepeats(test$)
prod$ = ltrim$( str$(prod))
if anagram(test$, prod$)
print it1,"x ", it2, "x ", it3,
print "= ", prod
endif
endif
endif
next it3
next it2
next it1
print: print "That's all, folks!"
print: print "Press any key to close ..."
do: until inkey$ <> ""
closeconsole
end
sub noRepeats(n$)
' if all characters in n$ are unique,
' returns -1, else returns 0.
def flag, i, j: int
def i$, j$: string
i = 1
flag = -1
do
i$ = mid$(n$, i, 1)
j = i + 1
do
j$ = mid$ (n$, j, 1)
if i$ = j$
flag = 0
endif
j = j + 1
until flag = 0 | j > 6
i = i + 1
until flag = 0 | i > 5
return flag
sub anagram(t$, p$)
' If all characters in t$ are contained
' once and once only in p$, regardless of
' order, returns -1, otherwise returns 0.
def flag, i, j: int
def i$, j$: string
for i = 1 to 6
i$ = mid$(t$, i, 1)
j = 1
flag = 0
do
j$ = mid$ (p$, j, 1)
if i$ = j$
replace$(p$, j, 1, "X")
flag = -1
endif
j = j + 1
until flag = -1 | j > 6
next i
if p$ = "XXXXXX"
return -1
else
return 0
endif
PROGRAM NOTES
The variables it1, it2, and it3 are created as iterators, each holding one of the three 2-digit integers to be investigated. These are run through using three nested FOR loops. Each loop is initialised in such a way that only unique integers are generated.
At each loop, the iterators are copied into the strings it1$ , it2$, and it3$, ready for use if the iterator product is suitable. This is checked by multiplying them together and comparing their product, prod, with 99999. It it's not greater, no anagram of the iterators can exist and that product is discarded. If it is greater, further checks can commence.
test$ is formed by concatenating it1$, it2$, and it3$ . In order to ensure there are no repeated digits, test$ is now passed as the argument to function noRepeats. If -1 (true) is returned, this string is suitable for comparison with the product of the iterators.
That product, prod, is copied into the string prod$ and, together with test$, passed as arguments to the function anagram . If -1 (true) is returned, we have a valid solution, and it's printed out to the screen.
The function noRepeats works by comparing every single character of the input string n$ with every other character. If a match is found at any time, flag is set to zero (false), the search stopped, and flag's value returned to the calling routine. If the search is completed while flag's value is still set to -1 (true), this will be returned instead to indicate that all characters are unique.
The function anagrams takes the two strings to be compared, t$ and p$ as arguments. Iterator i is used to work through each character of t$ one at a time. At each stage, iterator j works through p$ one character at a time. This means that every character of each string is compared with every other.
Whenever a match is found, the appropriate character in p$ is replaced by the character "X". This ensures it doesn't get matched again if the same character appears twice in t$.
At the end of this process, if a match of all characters between the two string was found, p$ will consist of "XXXXXX". If not it will have no more than 5 X's. In the former case, anagrams will return -1 (true), otherwise zero (false).
When you run the code, it pro-
duces the inset screen. There
are 8 solutions in all, and three-
quarters of them, not surprising-
ly, include a zero somewhere.
Solutions 1 and 3 are anagrams
of each other. Hmmm, looks like there may be another Puzzlet in there somewhere ...
Checking ...
20 x 79 x 81 = 127980
21 x 76 x 80 = 127680
28 x 71 x 90 = 178920
31 x 60 x 74 = 137640
40 x 72 x 86 = 247680
46 x 72 x 89 = 294768
49 x 50 x 81 = 198450
56 x 87 x 94 = 457968
That's all, folks!
Press any key to close ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.