If a 15-digit number is given, then what is the best way to find the next palindrome?

in C ++, what would be the fastest logic to find the next palindrome of a given 15-digit number? For example, what will be the following palindrome: 134567329807541?

+6
c ++ algorithm palindrome
source share
5 answers
  • Divide the number into three parts, head , mid , tail

    1345673 2 9807541

  • Reverse head and compare it to tail 3765431

  • If reverse(head) <= tail (if they are equal, the initial input is a palindrome, and you want the next one)

    • If mid < 9 , increment mid
    • The rest is incrementing the head part and set mid := 0
  • result: = head mid reverse(head) .

    1345673 3 reverse (1345673) => 134567333765431

+16
source share

I think that

  • Divide the number into three parts. 1345673 2 9807541.
  • Flip the last number 1457089
  • If it is larger than the first part (in this case)
    • firstpart ++
    • middlepart = 0
  • turn the first part over and replace the last part.
+3
source share

I am not going to implement anything, but I assume that the logic will be as follows:

  • Divide the number in the middle of the line: X is the left side and Y is the right side.
  • Let X '= {X + 1 if the converse is (X) Y; X otherwise}
  • The result is concat (X ', reverse (X'));

If the length is uneven, you need to process the middle digit separately. But this is pretty trivial.

+1
source share

I think the following algorithm should work too. It is easier to implement as well.

  i) Divide the given nos into three parts HEAD MID TAIL ii) Add 1 to number HEAD MID (in case of carry, follow basic addition rules) iii) reverse the new HEAD(store it in HEAD_REV) iv) required ans is:- 'new HEAD' MID HEAD_REV 

Hope the following example helps to better understand algo

let nos: - 23469 9 12367

  So HEAD -> 23469 MID -> 9 TAIL --> 12367 step 2:- 23469 9 +1 = 23470 0 (now HEAD -> 23470 MID -> 0 HEAD_REV -> 07432 ) 

Ans required: -
23470 0 07432

Plz do infrom me if there is a flaw in this procedure

0
source share
 Split the number into three parts head, mid and tail if reverse(head)>tail result := head mid reverse(head) else if reverse(head)= tail && mid<9 mid++ result := head mid tail else mid =0 head++ result := head mid reverse(head) 
0
source share

All Articles