THE PUZZLET PAGE


PUZZLET 88 - THE SOLUTION



' Puzzlet #088
' For a certain 5-digit integer abcde,
' whose digits are all different,
' ab * de - c = 1000. One example is
' 14872, because 14 x 72 - 8 = 1000.
' Find all the other 5-digit integers
' which fit this formula.
' By Dave Ellis.

declare AllDigsDifferent(a: int)
declare PrintIt()
def ab, c, de, newNum: int
openconsole
print "Checking ... ": print

for c = 0 to 9
    for ab = 10 to 99
        for de = 10 to 99
            if ab * de - c = 1000
            newNum = 1000*ab + 100*c + de
                if AllDigsDifferent(newNum)
                    PrintIt()
                endif
            endif
        next de
    next ab
next c

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

sub AllDigsDifferent(a)
'checks that all the digits in argument
'a are different.  If so, returns TRUE
'(-1), else returns FALSE (0).
def flag, it1, it2, L: int
a$ = ltrim$(str$(a))
L = len(a$)
it1 = 1
flag = -1
do   
    it2 = it1 + 1
    do
        p$ = mid$(a$, it1, 1)
        q$ = mid$(a$, it2 ,1)
        if q$ = p$
            flag = 0
        endif
        it2 = it2 + 1
    until flag = 0 | it2 > L
    it1 = it1 + 1
until flag = 0 | it1 > L - 1
return flag


sub PrintIt
'pretty printer
print newNum,
print "--> ", ab,
print "x ", de,
print "- ", c,
print "= 1000"
return



PROGRAM NOTES

The letters used to describe the problem are used for variable names to describe the same things - ab, c, de.  When these are concatenated, they form a new number, held in variable newNum.

The code is simplicity itself.  The three main variables are iterated in three nested for-next loops over the appropriate ranges.  There will be some duplication along the way, but the program runs so fast it isn't worth adding the extra code to prevent that.

For each combination of iterators, the test is made to see if ab*de - c equals 1000.  If it does, we have a potential solution.  However, the way the program's written, the result may have duplicated digits, which are excluded from the required results. 

So each time the magic 1000 is reached, the combination generating it, newNum, is submitted as parameter to the function AllDigsDifferent(). We have met this before in previous Puzzlets.  Please refer back if you want details of how it works, or alternatively e-mail me directly.

If AllDigsDifferent() returns TRUE, we have a legal solution, and subroutine PrintIt() is invoked to print the details to the computer screen.


When you run the program, you'll see the inset printed out onto your computer's screen.

You can see that each number is followed by its own anagram.

 

Checking ...

17359
--> 17 x 59 - 3 = 1000
59317 --> 59 x 17 - 3 = 1000
19753 --> 19 x 53 - 7 = 1000
53719 --> 53 x 19 - 7 = 1000
14872 --> 14 x 72 - 8 = 1000
72814 --> 72 x 14 - 8 = 1000

No more!

Press any key to exit ...





Site design/maintenance: Dave Ellis E-mail me!
Last Updated: May 9th, 2004.