Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need algorithm to find the nth palindromic number

consider that

    0 -- is the first
    1 -- is the second
    2 -- is the third
    .....
    9 -- is the 10th
    11 -- is the 11th

what is an efficient algorithm to find the nth palindromic number?

like image 254
Merna Avatar asked Feb 22 '13 08:02

Merna


1 Answers

I'm assuming that 0110 is not a palindrome, as it is 110.

I could spend a lot of words on describing, but this table should be enough:

#Digits #Pal. Notes
   0     1     "0" only
   1     9     x     with x = 1..9
   2     9     xx    with x = 1..9
   3    90     xyx   with xy = 10..99 (in other words: x = 1..9, y = 0..9)
   4    90     xyyx  with xy = 10..99
   5   900     xyzyx with xyz = 100..999
   6   900     and so on...
like image 128
Sjoerd Avatar answered Oct 01 '22 22:10

Sjoerd