| '
Find three different 2-digit integers, ' each of which is an odd number, and ' whose product contains the original ' 6 digits in any order. ' Example: 21 x 61 x 95 = 121695. ' By Dave Ellis. declare IsAnagram(str1$: string, str2$: string) declare PrintIt(p1: int, p2: int, p3: int, p4: int) def it1, it2, it3, prodInts: int def it1$, it2$, it3$, it$, prod$: string openconsole print "Searching ...": print for it1 = 11 to 95 #2 it1$ = ltrim$(str$(it1)) for it2 = it1 + 2 to 97 #2 it2$ = ltrim$(str$(it2)) for it3 = it2 + 2 to 99 #2 it3$ = ltrim$(str$(it3)) it$ = it1$ + it2$ + it3$ prodInts = it1*it2*it3 prod$ = ltrim$(str$(prodInts)) if IsAnagram (it$, prod$) PrintIt (it1, it2, it3, prodInts) endif next it3 next it2 next it1 print: print "No more!" print: print "Press any key to exit ..." do: until inkey$ <> "" closeconsole end sub IsAnagram(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 PrintIt (p1, p2, p3, p4) ' pretty printer print p1,"x ", p2, "x ", print p3, "= ", p4 return |
| PROGRAM NOTES The three iIterators it1, it2, and it3 are used to explore all legal combinations of the three desired odd-valued, two-digit, integers. Each starts off as an odd number and is incremented by 2 to ensure it remains odd-valued. For each combination to be examined, the integers are converted to string form it1$, it2$, and it3$ for manipulation. A further string form it$ is created which concatenates the values into one single value. For example if the three strings were "23", "31", and "95" then it$ would be "233195." it$ contains all the digits used in the three 2-digit integers being examined at this time. If these turn out to be a "scrambled" version of the product of the three integers, we have a solution. So the next step is to find the product. This is calculated and stored in prodInts, then converted into its string form, prod$, ready for the comparison with it$. prod$ and it$ are passed as parameters to the function IsAnagram() for comparision. If both strings contain the same digits regardless of order, we have a solution, and TRUE (-1) is returned. When this occurs, subroutine PrintResult() is called to print the answer neatly to your computer's screen. That's all there is to the program, folks. The subroutine IsAnagram() has been used many times in previous Puzzlets, so, if you need any information on how it works, please refer back in the Archives or just e-mail me directly. |
| As
you can see from the inset screen dump, the code finds three solutions
altogether (including the original example). Oddly, they're all divisible by 5. Is there a reason for that ... ? |
|
Searching ... 21 x 61 x 95 = 121695 29 x 75 x 91 = 197925 57 x 75 x 93 = 397575 No more! Press a key ... |
| Site design/maintenance: Dave Ellis | E-mail
me! |
Last Updated: April 11th, 2004. |