April 24, 2024, 02:46:30 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Puzzlet #37

Started by GWS, March 30, 2014, 12:45:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi,

Here's another of Dave Ellis's puzzles ..  :)

This one is to 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).


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()


All the best, :)

Graham

Tomorrow may be too late ..