By Kardi Teknomo, PhD.


< Previous | Next | Content >

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.


< Previous | Next | Content >

Share and save this tutorial
Add to: Del.icio.us Add to: Digg Add to: StumbleUpon Add to: Reddit Add to: Slashdot Add to: Technorati Add to: Netscape Add to: Newsvine Add to: Mr. Wong Add to: Webnews Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Simpy Add to: Furl Add to: Yahoo Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Blinkbits Add to: Ma.Gnolia Information

These tutorial is copyrighted .

Preferable reference for this tutorial is

Teknomo, Kardi. (2012) Palindrome. http://people.revoledu.com/kardi/tutorial/Palindrome/