5

I think the question is simple enough to understand.For more clarity I'm giving example :

In the list of 2 digit palindromes, the 7th palindrome is 77 (1st being 11, 2nd being 22 and so on).

Obviously a brute force solution exists but it's not efficient.

Can anyone suggest me some better solution to solve the problem ?

dark_shadow
  • 3,503
  • 11
  • 56
  • 81
  • the 7th 4 digit palinrome would be 0770 ? – Gir Aug 12 '12 at 21:02
  • No,you can't have leading zeros in a palindrome. – dark_shadow Aug 12 '12 at 21:03
  • If leading zeroes were allowed then 77 would be the eighth two-digit palindrome. As 77 is the seventh two-digit palindrome (as per task description) leading zeroes are not allowed. – TaZ Aug 12 '12 at 21:05
  • 2
    it something like number=n+10(^length/2)-1 and then convert it to a string with sprintf or something + add the reverse – Gir Aug 12 '12 at 21:08
  • Just to nitpick: the "nth n-digit palindrome" isn't the problem you describe...You probably mean the "nth m-digit palindrome" :) – Rody Oldenhuis Aug 13 '12 at 06:43

5 Answers5

18

First, we can simplify the problem because we only need to look at the first half of the digits (rounding up if there are an odd number of digits). I will call the first set of digits significant digits and the rest non-significant digits.

This is because the non-significant digits must match the significant digits (in reverse). It is not possible to have another palindrome number with the same leading significant digits and different non-significant digits. The significant digits determine the entire palindrome number.

Now, we just need to come up with an algorithm to generate the nth valid significant digits. This would be easier if we allowed for leading zeros, so we'll come up with the algorithm that allows for leading zeros, then tweak the algorithm.

The first few palindromes (significant digits) would be:

  • 1: 0000
  • 2: 0001
  • 3: 0002
  • ...
  • 100: 0099

So we can find the significant digits of the nth number by finding the decimal representation of (n-1).

To tweak the algorithm to work when not allowing leading zeros, we would start with a one as the leading digit:

  • 1: 1000
  • 2: 1001
  • 3: 1002
  • ...
  • 100: 1099

This boils down to finding the decimal representation of (n-1) + 1000 = n + 999 and expanding into a full palindrome:

Example: Find the 113th palindrome of length 9.

  • Determine number of digits to look at: Round up(9 / 2) = 5 --> only look at first 5 digits.
  • Find number to add to get rid of leading zeros: 10^(5-1) = 10000
  • Use formula: (113 - 1) + 10000 = 10112
  • Expanded into palindrome: 101121101

On a side note, this algorithm could also be generalized to finding the nth palindrome of any ordered set of symbols (or alphabet).

Generalized algorithm:

Given: finding palindrome number n , palindrome has m symbols as digits , there are p symbols (10 symbols in decimal)

  • Let q = ceiling(m / 2)
  • Let offset = p ^ (q - 1)
  • Let number = (n - 1) + offset
  • Let answer be number expanded as a palindrome
ryucl0ud
  • 622
  • 4
  • 7
5

The first few 7-digit palindrome are:

  • 1000001
  • 1001001
  • 1002001
  • 1003001
  • ...
  • 1009001
  • 1010101
  • 1011101
  • ...

I think it's very easy to see from the pattern of what is the nth m-digit palindrome...

Brian J
  • 694
  • 1
  • 21
  • 34
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

When the number of digits is even, just take the nth number with half as many digits starting from 100..0, where the length is half the number of digits. The palindrome is just this number followed by its mirror.

For an odd number of digits, just take the ceiling of half that number, and count from 100...0 the same way. Then the palindrome is this number followed by its mirror with the first digit removed.

The 65th 10 digit number:

65 + 9999 = 10064

1006446001

The 10298th 13 digit number:

10298 + 999999 = 1010297

1010297920101

UncleO
  • 8,299
  • 21
  • 29
0

For two digit palindromes the difference between two consecutive palindromes is 11.

Deepeshkumar
  • 395
  • 3
  • 13
-2

something like:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;

void reverseString(char *dest,char *src){
 char *iter=src;
 while (*iter) iter++;
 while (iter!=src){
  iter--;
  *dest=*iter;
   dest++;
 }
 *dest=0;
}

char *pal(int n,int len){
  char *tmp=new char[len/2+2];
  char *out=new char[len+1];
  sprintf(out,"%i",n+int(pow(10.0,(len+1)/2-1))-1);
  reverseString(tmp,out); //copy reversed out into tmp
  strcat(out,tmp+len%2);
  delete []tmp;
return out;
}

int main(){
 cout<<pal(4,7)<<endl;
}
Gir
  • 839
  • 5
  • 11