' Puzzlet #045
' Find two different integers, both less
' than 100, such that the difference of
' their squares is a perfect cube, and the
' difference of their cubes is a perfect
' square.
' By Dave Ellis.
declare DiffSquaresIsCube(a: int, b: int)
declare DiffCubesIsSquare(a: int, b: int)
declare PrintResult()
def it1, it2: int
openconsole
print "Searching ...": print
for it1 = 1 to 98
for it2 = it1 + 1 to 99
if DiffSquaresIsCube(it1, it2)
if DiffCubesIsSquare(it1, it2)
PrintResult()
endif
endif
next it2
next it1
print: print "No more!"
print: print "Press a key to exit ... ",
do: until inkey$ <> ""
closeconsole
end
sub DiffSquaresIsCube(a, b)
' if b^2 - a^2 is a perfect cube,
' returns 1, otherwise returns 0
def flag: int
def c: float
c = (b^2 - a^2)^(1/3)
if c = int(c)
flag = 1
else
flag = 0
endif
return flag
sub DiffCubesIsSquare(a, b)
' if b^3 - a^3 is a perfect square,
' returns 1, otherwise returns 0
def flag: int
def c: float
c = sqrt(b^3 - a^3)
if c = int(c)
flag = 1
else
flag = 0
endif
return flag
sub PrintResult
' pretty printer
def DiffSquares, DiffCubes: int
def it1$, it2$: string
it1$ = ltrim$(str$(it1))
it2$ = ltrim$(str$(it2))
DiffSquares = it2^2 - it1^2
DiffCubes = it2^3 - it1^3
print it2$ + "^2 - " + it1$ + "^2 = ",
print Diffsquares, "(",
print ltrim$(str$(exp(log(DiffSquares)/3))),
print "^3)"
print it2$ + "^3 - " + it1$ + "^3 = ",
print it2^3 - it1^3, "(",
print ltrim$(str$(sqrt(DiffCubes))),
print "^2)"
return
PROGRAM NOTES
Variable it1 and it2 are used in nested FOR loops to generate every possible combination of two different integers both of value less than 100.
Each pair of integers is submitted to the function DiffSquaresIsCube(), which returns a 1 (TRUE) if the difference of the squares of the arguments it1 and it2 is a cube, otherwise a 0 (FALSE) is returned.
If this test returns TRUE, the same two arguments are submitted to the function DiffCubesIsSquare(), with the same use of 1 and 0 in the return.
If this second test returns TRUE, we have a solution, and it's printed out by the subroutine PrintResult().
The process is repeated until all legal combinations of it1 and it2 have been tried.
When you run the code, it will produce the inset screen very quickly.
The integer pair 10 and 6 form the only solution.
Searching ...
10^2 - 6^2 = 64 (4^3)
10^3 - 6^3 = 784 (28^2)
No more!
Press a key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.