THE PUZZLET PAGE

PUZZLET 042 - THE SOLUTION


' Puzzlet #042
' To find an integer n such that, together
' with its double (2n) and its treble (3n),
' it uses all the digits 1 to 9 inclusive
' once and once only, thus making a 9-digit
' pandigital.
' By Dave Ellis.


declare PanDigital_9(p$: string)
declare PrintResult()
def n:
int
def n$, n2$, n3$:
string
def digit$:
string
openconsole
print
"Searching ...": print
print
" n   2n   3n": print
n = 123

do
    n$ = ltrim$(str$(n))
    n2$ = ltrim$(str$(2*n))
    n3$ = ltrim$(str$(3*n))
    digit$ = n$ + n2$ + n3$
    if PanDigital_9 (digit$)
        PrintResult()
    endif
    n = n + 1
until len(digit$) > 9


print: print "No more!"
print: print "Press a key to exit ..."   
do: until inkey$ <> ""
closeconsole
end


sub PanDigital_9(p$)
' if p$ is a 9-digit pandigital,
' returns returns 1, else returns 0.

def count, flag, i, L: int
L = len(p$)
i = 1
flag = 1
flag_no_zeroes = 1
do
    if mid$(p$, i, 1) = "0"
        flag_no_zeroes = 0
    endif
    i = i + 1
until flag_no_zeroes = 0 | i > 9
if L <> 9 | flag_no_zeroes = 0
    flag = 0
else
    test$ = "123456789"
    i = 1
    do
        i$ = mid$(p$, i, 1)
        j$ = mid$(test$, val(i$), 1)
        if j$ = i$
            replace$(test$, val(i$), 1, "X")
        endif
        i = i + 1
    until flag = 0 | i > 9
    if test$ <> "XXXXXXXXX"
        flag = 0
    endif
endif
return
flag

sub PrintResult
' pretty printer
print n$, "  ", n2$, "  ", n3$
return


PROGRAM NOTES

W
e need to begin by determining the search range.   How many digits can n have?  If it's just one digit, it's impossible for the digits of n, 2n, and 3n to comprise nine digits altogether.  How about making n two digits in length? The largest possible value for n would then be 99. 99,  198, and 297 still compise less than nine digits altogether.

Let's try three digits for n.  The greatest value possible is 999.  999, 1998, and 2997 comprise more than nine digits altogether, so n will be less than 999 but must be three digits in size.

The minimum value for n is obviously 123, since this is the smallest possible 3-digit integer whose digits are all different.  

It's not necessary to calculate the maximum value for n in advance, since the program can be made to stop as soon as it detects that n has caused a number comprising more than nine digits to be generated.

The code commences by creating three strings, n$, n2$, and n3$n, 2n, and 3n.  These are then concatenated into a single string, digit$.
digit$ is passed to the function Pandigital_9(), whose job is to return 1 (TRUE) if digit$ is a 9-digit pandigital, otherwise 0 (FALSE).

If TRUE is returned, the result is printed out.  Either way, the value in n  is incremented by 1.  The last value of digit$ is inspected now.  If its length was greater than nine characters, the program ends.  If not, the DO loop is executed again with the newly-incremented value of n. This goes on until the length of digit$  exceeds nine.

The Pandigit_9 function has been used in earlier Puzzlets.  If you would like a description of it, please refer back through the archives or e-mail me at the address below.
 

When you run the code,  it will produce the inset screen.

There are 4 solutions. Strangely, for the first two, each corresponding integer is an anagram of the other. Even odder, the same is true for the last two solutions as well.

Searching ...

  n     2n    3n

192  384  576
219  438  657
273  546  819
327  654  981

No more!

Press a key ...


MAIN MENU
DOWNLOAD CODE
ARCHIVES


Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.