THE PUZZLET PAGE

PUZZLET 052 - THE SOLUTION


' Puzzlet # 052
' To find non-palindromic integers whose squares
' are palindromic, up to a maximum value for the
' square of one billion (10^9).
' By Dave Ellis.


declare IsPalindrome(p: int)
declare PrintResult(n: int, s: int)
def num, square: int
openconsole
print
"Searching ...": print
print
"Number    Number^2": print
num = 10

do
    if not
(IsPalindrome(num))
        square = num * num
        if IsPalindrome(square)
            PrintResult(num, square)
        endif
    endif

    num = num + 1
until num > 31622

print: print "No more!"
print: print "Press any 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(n, s)
' pretty printer
print using ("######", n), "   ",
print using ("#########", s)
return


PROGRAM NOTES


To make the explanation of this code simpler, I've repeated the lines that control the program with line numbers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

num = 10
do
    if not
(IsPalindrome(num))
        square = num * num
        if IsPalindrome(square)
            PrintResult(num, square)
        endif
    endif

    num = num + 1
until num > 31622



Line 1:  Variable num is used to hold the integer value being investigated at any one time.  It's initialised to 10 to avoid the trivial single-digit palindromes.

Line2:  Opens the main program loop.

Line 3:  When num has been squared, we'll want to know if it's a palindrome, and one of my standard functions, IsPalindrome(), has been imported to do this for us.  However, before we do that, we want to know the opposite about num itself - is it NOT a palindrome?  We can use the IsPalindrome() function for this too, literally by just sticking a
not in front of it.

Line 4:  This line is only run on those occasions when num turns out not to be non-palindromic. It squares num and stores the result in variable square, ready for the next part of the test in line 5.

Line 5:  Tests square for palindromicity (what a mouthful that word is).

Line 6:  This is the payoff.  If num wasn't a palindrome and square was, we have a valid solution which needs to be printed out. The subroutine PrintResult() is called to do that.

Lines 7 and 8:  Housekeeping - closing the if-endif clauses.

Line 9:  Increments num ready for the next test.

Line 10: Decision time.  Should we execute the main loop again? If num is greater than 31,622, square will be more than one billion, and we don't need to continue.  If it's less than that number we can carry on.

The function IsPalindrome() is an old friend, and doesn't need further explanation here.  If you want to know more about it, please either refer back to archived puzzlets or just e-mail me at the address below.

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

Every number on the left is a non-palindrome. In each case, its square is shown on the right, and is a palindrome.

There are no other solutions.

Searching ...

Number    Number^2

        26               676
      264            69696
      307            94249
      836          698896
    2285         5221225
    2636         6948496
  22865      522808225
  24846      617323716
  30693      942060249

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.