Square Palindromes

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

This program  investigates finding  the smallest ":even"  palindrome which is the square of another integer. 

Background & Techniques

A viewer recently sent me a problem asking about the smallest even palindrome which is the square of another integer. We both solved the problem but came up with different answers. It turned out that the reason was a matter of definitions.  I assumed that  an "even" palindrome would be one that contained all even digits. Viewer Geoff defined it as one with an  even number of digits.  Since  4, met my definition, the problem seemed too easy so I searched for the smallest greater that 10.

Two buttons in the program  provide answers for each of the definitions.

As an added bonus, I raised the question about palindromes which meet both conditions (perfect square palindromes containing an even number of even digits). It turns out that there are no solutions within the range of 64 bit integers (approximately 20 digits).   But I did get a chance to compare  two methods for testing digit counts, testing for even digits, and whether they form a palindrome.  The third button calculate the times 3 search methods  using variations of the techniques.   

Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now.

Programmer's Notes:

First Button:

For the first button, the even digit values solution, I use the IsPalindrome function from our MathsLib unit included in the DFF Library.   Variable "i" loops from 1 to Maxmaxi (set to 1,000,000).   Just for fun, it counts the number of values in this range which are palindromes. 

 
 Proof:  If i is odd, it can be written as 2n+1 for some n. Therefore  i2 = (2n+1)*(2n+1).  which (remembering the FOIL method fo multiplying binomials) = 4n2+4n+1 which is odd.  QED  

For the real task we  call IsPalindrome only for the squares of even i  values (i is even if i mod 2 =0).    We only need to check odd values of i because odd values squared will always be odd.  See the side bar for proof.  Function IsAllEven takes the string version of each palindrome and checks whether all characters are even.    Since, the numeric values of the characters '0' through '9' have the same ordinal value as the digits 0 through 9,  we can test ord(ch) mod 2 for each character, ch,  of the string.

 

Second button:

The second button is easy - pass a squared integer to the numeric input version of IsEvenPalindrome which will build an integer array of the digits in the number then check if the count is even and if each of the digit values in the array is even.  The function  converts the candidate  in a loop extracting the low order digit with the modulus remainder function (mod), then dividing the candidate by 10 to remove that digit until the candidate reaches 0. Extracted digits are saved in an array for further testing.

Third Button:

The third button originally tried tried to find overlap between the solutions form the two previous buttons.  There are none within the range tested (up to 10^12) but I did convert the code to test the timing of the two calculation methods .  A second "Overload" vesion of IsEvenPalinDrome was written to accept a string version of the number for testing.  This version using  IntToStr testing turned out to be more than twice as fast, testing the first 1,000,000 perfect squares (up to 12 digits) in about 0.6  second on my Dell Studio 17 laptop compared to 1.4 seconds for the digital array version..
 

Running/Exploring the Program 

bulletDownload  executable
bulletDownload source  (Note: the IsPalindrome  function resides in the MathsLib unit t which resides in our library zip file of commonly used units.  Library file DFFVLIB13 or later is required to recompile this program )
bulletDownload current library file  (DFFLibV15) required to recompile SquarePalindromes.

 

Suggestions for Further Explorations

???
   

 

Original:  March 19, 2012

Modified:  May 15, 2018

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.