' Puzzlet #073
' 59 multiplied by 845 gives 49855, an
' anagram of 59 and 845. The sum of the
' digits involved is 31 . Find any other
' pairs of numbers (one 2-digit and one
' 3-digit) whose product is an anagram
' of the original digits, and having 31
' as the sum of its digits.
' By Dave Ellis.
declare SumStringDigits(s$: string)
declare Anagram (str1$: string, str2$: string)
declare PrintResult()
def digs2, digs3, prod: int
def dig2$, dig3$, dig5$, product$: string
openconsole
print "Searching ... ": print
print "Num1 Num2 Product SumDigits"
for digs2 = 10 to 99
dig2$ = ltrim$(str$(digs2))
for digs3 = 100 to 999
dig3$ = ltrim$(str$(digs3))
dig5$ = append$(dig2$, dig3$)
if SumStringDigits(dig5$) = 31
prod = digs2 * digs3
product$ = ltrim$(str$(prod))
if Anagram (dig5$, product$)
PrintResult()
endif
endif
next digs3
next digs2
print: print "No more!"
print: print "Press any key to exit ... ",
do: until inkey$ <> ""
closeconsole
end
sub anagram(str1$, str2$)
' if str1$ and str2$ are anagrams of
' each other, 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 SumStringDigits(s$)
' returns the sum of the digits
' within s$
def i, L, sum: int
L = len(s$)
sum = 0
for i = 1 to L
sum = sum + val(mid$(s$, i, 1))
next i
return sum
sub PrintResult
' pretty printer
print " ", digs2, "x ", digs3,
print "= ", prod,
print " ", dig5$
return
PROGRAM NOTES
2-digit and 3-digit integers are stored in variables digs2 and respectively. Their product is stored in variable prod. In order to manipulate them, they need to be in string form. Variables dig2$, dig3$, and product$ are used for this purpose. At one point, we're going to need to concatenate dig2$ and dig3$. The variable dig5$
Using two nested loops, digs2 is cycled through all its possible values from 10 to 99; within this loop, digs3 is cycled through all values from 100 to 999. This gives every possible combination of 2-digit and 3-digit integers.
For every combination, the values are concatenated and stored in dig5$. The latter is passed to the function SumStringDigits(). If the returned value is 31, we have a potential solution, and further processing is indicated.
The product of the two values currently being investigated is stored in product$. dig5$ and product$ are passed as parameters to the function Anagram(). If one's an anagram of the other, TRUE (-1) will be returned, which means we have a solution. In this case, subroutine PrintResult() is called and the results are printed out in a neat format to the computer screen.
Function Anagram() has been used before in Puzzlet and its operation described in detail. Please refer back in the archives if you want to see the detailed description, or e-mail me at the address below. is created specifically to hold the 5-digit result.
When the program is run, it generates the inset screen.
There are thus five answers altogether. Note that the sum of the digits of each answer is always 31, as required.
Searching ...
Num1 Num2 Product SumDigits
59 x 845 = 49855 59845
65 x 875 = 56875 65875
65 x 983 = 63895 65983
68 x 926 = 62968 68926
89 x 482 = 42898 89482
No more!
Press any key to exit ...
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.