THE PUZZLET PAGE

PUZZLET 033 - THE SOLUTION

' Puzzlet #033.
' Find a 5-digit integer abcde such that
' a^a - b^b + c^c - d^d + e^e = abcde + 1
' By Dave Ellis.


declare LoadArray()
declare EvalArray()
declare PrintResult()
def flag, n, v: int
def array[6]: int
openconsole
print
"Searching ..."
n = 10000
flag = 0

do
    LoadArray()
    v = EvalArray()
    if n + 1 = v
        flag = 1
    endif
    n = n + 1
until flag

PrintResult()
print: print "Press a key to exit ... ",
do: until inkey$ <> ""
closeconsole
end

sub
LoadArray
' put each of the digits comprising n into
' a separate cell of the array

def i, j: int
j = n
for i = 5 to 1 #-1
    array[i] = j % 10
    j = j/ 10
next i
return

sub
EvalArray
' turns abcde into a^a - b^b + c^c -d^d + e^e
def i, sign, value, power: int
value = 0
sign = -1
for i = 1 to 5
    sign = sign * -1
    power = array[i]^array[i]
    value = value + sign*power
next i
return value

sub PrintResult
' pretty printer
def i: int
def array$, sign$: string
sign$ = " -+-+"
print: print n - 1, "+ 1 = ",
for i = 1 to 5
    print " " + mid$(sign$, i, 1) + " ",
    array$ = ltrim$(str$(array[i]))
    print array$ + "^" + array$,
next i
print
return


PROGRAM NOTES

The program can best be understood by following the same approach as used in its design:  top-down.  Everything is controlled by the main routine, shown below with line numbers added to assist in the explanation.

1
2
3
4
5
6
7
8

do
    LoadArray()
    v = EvalArray()
    if n + 1 = v
        flag = 1
    endif
    n = n + 1
until flag


Line 1:  The DO loop between lines 1 and 8 controls the action.  It will execute continuously until variable flag is set, at which point the loop is exited and the program terminates.

Line 2: The subroutine LoadArray() is called. It takes the current value of the 5-digit integer variable n, splits it up into its individual digits and loads them into array[].  

Line 3:  The data loaded into
array[] by Line 2 is now manipulated by the function EvalArray() (for Evaluate Array) and stored in variable v. The task of this function is take the values of a, b, c, d, and e, stored separately in array[], and calculate aa - bb + cc - dd + ee.  The result is returned to the calling routine.

Line 4:  We have already evaluated variable n in the required manner and stored the result in variable v.  v is now compared with n + 1.

Lines 5 and 6:  If v was equal to n + 1, variable flag is set to -1, indicating TRUE.

Line 7:  n is incremented ready for the next test if it becomes necessary.

Line 8:  The DO loop now makes a decision.  If flag is set to TRUE, the loop is exited.  If not, the loop executes again, but this time with the newly-incremented value of n.


When you run the code,  it will produce the inset screen.   This is the only solution. 

Searching ...

52645 + 1 =  5^5 - 2^2 + 6^6 - 4^4 + 5^5

Press a key to exit ...

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

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