' Puzzlet #049
' Find a palindrome whose square can be
' derived from three consecutive integers,
' abc, using the process a + b^2 + c^3.
' Note that a, b, and c must all be less
' than 100.
' Example: 8 + 9^2 + 10^3 = 1089. The
' square root of 1089 is 33, a palindrome.
' Are there any others like this?
' By Dave Ellis.
declare IsPalindrome(p: int)
declare PrintResult(p: int, r: int, s: int, t: int)
def printline, num, square: int
def root: float
openconsole
print "Searching ...": print
print " n n+1 n+2 n+(n+1)^2+(n+2)^3 pal"
num = 1
printline = 5
do
square = num + (num + 1)^2 + (num + 2)^3
root = sqrt(square)
if root = int(root)
if IsPalindrome(root)
PrintResult(num, root, square, printline)
printline = printline + 1
endif
endif
num = num + 1
until num > 97
print: print "No more!"
print: print "Press a key to exit ... ",
do: until inkey$ <> ""
closeconsole
end
sub IsPalindrome(p)
' if p is a palindrome, returns 1,
' otherwise returns 0
def flag, i, j, L: int
def p$, q$, r$: string
p$ = ltrim$(str$(p))
L = len(p$)
j = int(L/2)
i = 1
flag = 1
do
q$ = mid$(p$, i, 1)
r$ = mid$(p$, L + 1 - i, 1)
if q$ <> r$
flag = 0
endif
i = i + 1
until flag = 0 | i > j
return flag
sub PrintResult(p, r, s, t)
' pretty printer
print using ("##", p)
locate t, 6: print using("##", p + 1)
locate t, 11: print using("##", p + 2)
locate t, 21: print using("######", s)
locate t, 34: print using("###", r)
return
PROGRAM NOTES
In order to make the program listing easier to follow, the following extract of the part of the code that controls everything is given line numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
num = 1
printline = 5
do
square = num + (num + 1)^2 + (num + 2)^3
root = sqrt(square)
if root = int(root)
if IsPalindrome(root)
PrintResult(num, root, square, printline)
printline = printline + 1
endif
endif
num = num + 1
until num > 97
Line 1: The variable num holds the first integer of the triplet to be explored. This line initialises it to 1.
Line 2: Getting a neat printout of the solutions to this Puzzlet is a little awkward. The variable printline assists here, and this line initialises it to 5.
Line 3: Opens the main DO loop.
Line 4: The Puzzlet is built around processing integers according to the expression a + b2 + c3, where a = num, b = (num + 1), and c = (num + 2). The variable square is used to hold the result after evaluation.
Line 5: The square root of square is taken and then stored in variable root.
Line 6: Checks to see if root is perfect (that is, has no decimal part) or not.
Line 7: If root is a perfect square root, this line goes on to check if it's also a palindrome. The function IsPalindrome() is called to perform this task.
Line 8: If we've indeed found a palindromic root, it's a valid solution and must be printed out. Subroutine PrintResult() carries this out.
Line 9: Variable printline() is incremented ready to print out any further solutions that are found.
Lines 10 and 11 just close the end-endif statements.
Line 12: Increments num ready for the next investigation.
Line 13: Checks the value of num. If it's greater than 97, then one or both of (num + 1) or (num + 2) will equal or exceed 100, which is specifically excluded in this problem. Therefore, if num isn't greater than 97, the DO loop executes again. If it is, the DO loop is exited and the program terminates.
The function IsPalindrome() is an old friend of this column, so I don't propose to describe how it works here. If you want to know more about it, either refer back to previous Puzzlets or e-mail me at the address given below.
When you run the code it will generate the inset screen.
There are only two solutions altogether.
Searching ...
n n+1 n+2 n+(n+1)^2+(n+2)^3 pal
8 9 10 1089 33
80 81 82 558009 747
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.