' Puzzlet #037
' Find an integer whose 3rd and 4th powers between
' them use all the digits 0 to 9 inclusive once
' and once only (making a 10-digit pandigital).
' By Dave Ellis.
declare PanDigital_10(p$: string)
declare PrintResult()
def done, n: int
def cube$, fourth$, sumPowers$: string
done = 0
n = 1
openconsole
print "Searching ...": print
do
cube$ = ltrim$(str$(n^3))
fourth$ = ltrim$(str$(n^4))
sumPowers$ = cube$ + fourth$
if PanDigital_10(sumPowers$)
done = 1
else
n = n + 1
endif
until done
PrintResult()
print: print "Press a key to exit ..."
do: until inkey$ <> ""
closeconsole
end
sub PanDigital_10(p$)
' if p$ is a 10-digit pandigital,
' returns 1, else returns 0.
def psn, flag, i, L: int
def i$, j$, test$: string
L = len(p$)
i = 1
flag = 1
if L <> 10: ' is it the right length?
flag = 0
else
test$ = "1234567890": 'template for right answer
i = 1
do
i$ = mid$(p$, i, 1)
if i$ = "0"
j$ = "0"
psn = 10
else
j$ = mid$(test$, val(i$), 1)
psn = val(j$)
endif
if j$ = i$
'blank out correctly-identified digit
replace$(test$, psn, 1, "X")
endif
i = i + 1
until flag = 0 | i > 10
if test$ <> "XXXXXXXXXX"
flag = 0
endif
endif
return flag
sub printResult()
' pretty printer
print ltrim$(str$(n)) + "^3 = ", cube$
print ltrim$(str$(n)) + "^4 = ", fourth$
print: print sumPowers$,
print " is a 10-digit pandigital."
return()
PROGRAM NOTES
This program can best be understood by looking at the main routine. Everthing else is there just to service it. For ease of description, it's shown below with line numbers.
1
2
3
4
5
6
7
8
9
10
do
cube$ = ltrim$(str$(n^3))
fourth$ = ltrim$(str$(n^4))
sumPowers$ = cube$ + fourth$
if PanDigital_10(sumPowers$)
done = 1
else
n = n + 1
endif
until done
All the action takes place between lines 1 and 10. This nicely illustrates one of the principles of top-down programming: it should be capable of being understood by anyone, even a non-programmer, and you can certainly say that of "do ... until done."
Variable done was preset to 0 (FALSE) prior to entering the main DO loop. So the program will run until something within the DO loop changes the value of done to 1 (TRUE).
Line 2 commences by processing the current value of variable n. Its cube is taken and converted into a string which is stored in aptly-named variable cube$.
Line 3 repeats the process but takes the fourth power instead of a cube, and stores the result in variable fourth$.
Line 4 concatenates the two strings found in the lines 2 and 3 and stores the result in variable sumPowers$. Concatenate means to join together without further processing. So "123" concatenated with "456" (written as "123" + "456") produces "123456" whereas 123 + 456 produces 579.
Line 5 passes the value of sumPowers$ to the function Pandigital_10, whose function is clear from the name: if the argument passed to it is a 10-digit pandigital, it will return 1 (TRUE) otherwise 0 (FALSE).
If FALSE is returned, the current value of n is obviously not the answer. In these circumstances, lines 7 and 8 are invoked. The value of n is incremented by 1 and the DO loop executed again.
If TRUE is returned, the current value of n is a valid answer. Line 6 sets done to 1 (TRUE). This is detected by line 10 and the DO loop is exited.
The subroutine PrintResult() is immediately called to print the answer to the screen, after which the program closes in an orderly manner.
The function PanDigital_10() is fairly weighty. This is one of my library functions, and as such has been used before in Puzzlet #020. If you want a detailed explanation of how it works, look up the solution to that Puzzlet (go to the Archives page and you'll see it listed there).
You can see from the printout below that the answer is found after only 18 iterations of the DO loop. This implies that it could have been found quickly by trial and error. You may be right! However, I suspect that many folk would take longer due to arithmetic mistakes etc.
When you run the code, it produces the inset screen.
This is the only solution.
Searching ...
18^3 = 5832
18^4 = 104976
5832104976 is a 10-digit pandigital.
Press a key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.