| x2 = abccba |
A numerical palindrome is any
number which reads the same forwards and backwards. |
|
We're interested here in
an integer
numerical palindrome, that is, it doesn't have a decimal point.
There's a particular 6-digit example that is also a perfect square. Can you find it? Are there any others like this? If so, what are they? As usual, there’s more than one way to skin this particular cat. The hard way is to generate all possible 6-digit palindromes (and it turns out there are 900 of them), and check each one individually to see if it happens to be a perfect square. An easier way is to generate all integers whose squares will be 6-digits in length, and check those squares for palindromicity. This will include (100,000)½ to (999,999)½ , which rounds as 317 to 999 inclusive, a range of 683. You can see this in action in the Ibasic program below. ' Puzzlet #004. ' To find 6-digit numerical palindromes ' which are also perfect squares. ' By Dave Ellis. ' Declarations declare Pal (n: int) def i: int ' Screen SetUp color 11, 0: cls openconsole print " Searching ..." color 14, 0: cls ' Main Routine for i = 317 to 999 if Pal(i*i) print str$ (i)+"^2 = ",i*i endif next i ' Finished color 11, 0 print: print " No more!" print: print " Press a key ..." do: until inkey$ <> "" closeconsole end ' +++ Subroutine and Functions +++ sub Pal(n) ' Returns -1 if n is a palindr- ' ome, ' otherwise returns 0. ' By Dave Ellis. def n$, p$, q$: string def flag, i: int n$ = ltrim$(str$ (n)) i = 1 flag = -1 do p$ = mid$(n$, i, 1): 'left char q$ = mid$(n$, 7 - i, 1): 'right char if p$ <> q$: 'left char = right char? flag = 0 endif i = i + 1 until i > 3 | flag = 0 return flag |
| PROGRAM NOTES In
the main routine, iterator i searches through the
range worked out above. At each value of i
, a check is made to see whether i 2
is a palindrome by calling the function Pal().
If it turns out to be a palindrome, it's printed out before the
next value of i is processed.
In the function Pal(), the argument n is turned into string n$ for bit-sliced processing. Variable flag is set to –1 (TRUE) and will remain at that value unless it is discovered that n$ isn’t actually a palindrome. The DO loop looks at pairs of digits in order. The first pair is digit 1 and digit 6. The second is digit 2 and digit 5, etc. At every step the digits are held in p$ and q$ for comparison. If at any time p$ isn’t equal to q$, n$ can’t be a palindrome, flag is set to 0 (FALSE) and the loop exited. If flag is never set to 0, the DO loop will eventually exit anyway when it’s looked at all digit pairs. In these circumstances, flag will still be set to TRUE. Whatever value is stored in flag is returned to the main routine at that point. |
| Running the program produces the inset screen. This shows clearly that the answer is 836, because 8362 is 698896, a palindrome. It also shows that there are no other 6-digit palindromes which are also perfect squares. | ![]() |
| The integer 698896 is interesting in another way: it forms yet another palindrome when read upside down! There’s another Puzzlet exploiting this property in the pipeline – so watch this space … |
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: January 23rd, 2010.