THE PUZZLET PAGE

PUZZLET 016 - THE SOLUTION
' Puzzlet #016
' Find a 5-digit integer such that the
' sum of the cube of the integer formed
' from its first 2 digits and the square
' of the integer formed from its last 3
' digits generates a new integer which
' is an exact multiple of the original
' integer.  Example:
' 38463: 38^3 + 463^2 = 269241 = 38463*7
' By Dave Ellis.


declare PrintIt()
def i, j, k, m: int
def n: float
openconsole
print
"Searching ...": print

for i = 10000 to 99999
  j = i/1000
  k = i % 1000
  m = j * j * j + k * k
    n = m/i
  if n - int(n) = 0
    PrintIt()
  endif
next
i

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


sub PrintIt ()
def p: int
print i, "-> ",
print using ("##", j),
print "^3 + ",
print using ("###", k),
print "^2 =",
print using ("########", m),
print " = ",
p = m/i
print using ("##", n),
print " * ", i
return

PROGRAM NOTES

Iterator i runs through all possible 5-digit integers.  For each integer, the first two digits are separated out into variable j by dividing i by 1,000.  Suppose i's value at this time is 12345.  Normally you would expect j to produce 12.345.  However, we previously declared j as an integer as part of the line:  
def i, j, k, m: int . This causes j to be truncated to 12, so we have effectively isolated the integer formed from the first two digits of i into j.

i is processed again by performing a modular division by 1,000 on it and putting the result  into variable k.  Modular division simply means "divide by a stated value, keep the remainder, and throw away the rest of the result."  When i is equal to 12345, dividing by 1,000 gives 12 remainder 345, so, in this case, k will hold 345.  We have effectively isolated the integer formed by the last three digits into k.

We need to know the sum of the cube of the integer formed by the first two digits and the square of the integer formed by the last three digits.  This is easily obtained and saved into variable m by the following line:  
m = j * j * j + k * k.

All that's needed now is to see if this new sum is an exactly multiple of the integer from which it was derived.  The line
n = m/i divides them as required and saves the result into variable n.  If there was no remainder from the division, n must be an exact multiple of i.  This is checked by line if n - int(n) = 0 .  If it really does equal zero, we have a valid solution, and it's printed out by subroutine PrintIt().  Either way, the program goes on then to look at the next value of i, and so on until every possible 5-digit integer has been investigated.


When you run the code,  it produces the inset screen.

It's rather curious that the first solution happens to be exactly 2 15, a very well known value indeed in the world of mathematics and computing.

And look at line 5:

773
+ 772 = 77077.

As usual, I can see the beginnings of another Puzzlet lurking slyly inside those facts somehow or other...


Searching ...

32768 -> 32^3 + 768^2 =  622592 = 19 * 32768
38463 -> 38^3 + 463^2 =  269241 =   7 * 38463
57399 -> 57^3 + 399^2 =  344394 =   6 * 57399
59177 -> 59^3 + 177^2 =  236708 =   4 * 59177
77077 -> 77^3 +   77^2 =  462462 =   6 * 77077
78741 -> 78^3 + 741^2 = 1023633 = 13 * 78741
86964 -> 86^3 + 964^2 = 1565352 = 18 * 86964
94987 -> 94^3 + 987^2 = 1804753 = 19 * 94987

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.