How to Generate Palindrome?
A palindrome is a word or a number that can be read the same way in either direction, forward or backward. In this section, you will learn how to generate a palindrome number.
Using the code to test palindrome that explain in the previous section, we can generate palindrome number from a minimum up to a maximum number. Generating a palindrome number is simply iterating the number from the minimum to the maximum values and test whether that number is a palindrome. If that number is a palindrome, we store in an array (that dynamically changes it size) and at the end send back to the caller.
Function GeneratePalindromeNumber(minNum, maxNum) ' return array of palindromes between minimum number to maximum number ' (c) 2011 Kardi Teknomo ' http://people.revoledu.com/kardi/ For i = minNum To maxNum If isPalindrome(i) Then k = k + 1 ReDim Preserve arr(1 To k) 'increase array size dynamically arr(k) = i End If Next i GeneratePalindromeNumber = arr End Function
Palindrome generation is useful for statistical and mathematical analysis. Using the code of palindrome generation above, try to experiments and answer the following questions:
- How many palindromes numbers are between 1 to 1 million?
- What is the distribution of palindrome number by its digits?
- What is the difference between a palindrome number and the next palindrome number? Is there any pattern in the difference of palindrome number?
In the next sections , we will modify the palindrome generation code above to search for the next palindrome.
Share and save this tutorial
Preferable reference for this tutorial is
Teknomo, Kardi. (2012) Palindrome. http://people.revoledu.com/kardi/tutorial/Palindrome/