THE PUZZLET PAGE

PUZZLET 050 - THE SOLUTION


' Puzzlet #050
' Find two consecutive 3-digit integers such
' that, in both cases, the last 3 digits of
' the integer's cube is the same as the
' integer itself.
' Example: 375^3 = 52734375, 376^3 = 53157376.
' Are there any other examples like this?
' By Dave Ellis.


declare MatchInCube(m: int)
declare PrintResult(p: int)
def count, num: int
openconsole
print
"Searching ...": print
num = 100

do
    count = 0
    do
        if MatchInCube(num)
            count = count + 1
        else
            count = 0
        endif
        num = num + 1
        if count > 1
            PrintResult(num)
        endif
    until
count > 1 | num > 998
until num > 998

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


sub MatchIncube(m)
' if last 3 digits of m match the last
' 3 digits of m^3, returns 1, otherwise
' returns 0

def flag, n: int
n = m^3
n = n % 1000
if n = m
    flag = 1
else
    flag = 0
endif
return flag

sub PrintResult(p)
' pretty printer
def p$, q$: string
p$ = ltrim$(str$(p - 2))
q$ = ltrim$(str$(p - 1))
print p$ + "^3 = ",
print using ("#########", (p - 2)^3),
print " : ", q$, "^3 = ",
print using ("#########", (p - 1)^3)
print
return


PROGRAM NOTES

In the main DO loop, variable count is used simply as a flag to indicate one of three things:

Value of count
Meaning
0
No match found
1
One match found
2
Two successive matches found

A match is found if the last three digits of variable num are equal to the last three digits of num itself.  The function MatchInCube performs this check, returning 1 (TRUE) or 0 (FALSE) as appropriate.

If no match is found, we are in the situation that either a match was found in the last-but-one check, or it wasn't.  In either case, we have to restart the count of matches to zero again.  When this happens, num (which was initialised to 100 earlier since we are only looking at 3-digit integers) is incremented and the new value of num is investigated.

If a match was found,  count is incremented.  If, as a result, count's value is now greater than 1, two consecutive matches have been found, and we have a solution.  Subroutine PrintResult() is called to print it out to the screen.

At this point, a decision has to be made as to whether to execute the inner DO loop again or not.  If count  is greater than one, it needs to be reset to zero and the search for the next consecutive pair started. This is accomplished by exiting the inner loop.  The outer loop will reset count to zero and start looking at the latest value in num.

The decision about the inner loop is also dependent on the value in num.  If the last increment took it above 998, it will no longer be possible to find two consecutive 3-digit integers, and this will also cause the program to quit the inner loop, regardless of count's value.

The outer loop also has to look at the value of num, and for the same reason as given above vis a vis the inner loop.  If num is greater than 998, the outer DO loop is exited, and the program stops.

When you run  the code, it generates the screen shown inset.

The 375 solution was given as an example.  624 is the only other one.


Searching ...

375^3 =  52734375 : 376^3 =  53157376

624^3 = 242970624 : 625^3 = 244140625

No more!

Press any key to exit ...


MAIN MENU
DOWNLOAD CODE
ARCHIVES


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