[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionThis program investigates finding the smallest ":even" palindrome which is the square of another integer. Background & TechniquesA 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. 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.
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
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |