1

Let's say I need to verify that a very long number is a palindromic number. Is there a formula that can be used to determine if it is one?

N. F. Taussig
  • 76,571
  • 1
    $n$ is a palindrome if and only if $n=\sum\limits_{k=0}^{\lfloor\log_{10}n\rfloor}\left(\Bigl\lfloor\frac{n}{10^{k}}\Bigr\rfloor\bmod{10}\right)\cdot10^{\lfloor\log_{10}n\rfloor-k}$ – barak manos Mar 07 '15 at 13:02

1 Answers1

2

We could formalize your problem as the following. If $n \in \mathbb{N} $ is palindromic, then it is true, that

$$n = r(n),$$

where $r : \mathbb{N} \rightarrow \mathbb{N}$ function gives the reverse of a number.

We can get $r$ like this:

$$r(n)=\sum_{\ell=0}^{m-1}10^{m-1-\ell}\left(\left\lfloor\frac{n}{10^\ell}\right\rfloor-10\left\lfloor\frac1{10}\left\lfloor\frac{n}{10^\ell}\right\rfloor\right\rfloor\right),$$

where $n$ is the number we want to reverse, and $m$ is the digits of $n$.

We know that $m$ is

$$m=\lfloor\log_{10}n\rfloor+1.$$

So using this we get for $r$ the following.

$$r(n)=\sum_{\ell=0}^{\lfloor\log_{10}n\rfloor}10^{\lfloor\log_{10}n\rfloor-\ell}\left(\left\lfloor\frac{n}{10^\ell}\right\rfloor-10\left\lfloor\frac1{10}\left\lfloor\frac{n}{10^\ell}\right\rfloor\right\rfloor\right),$$

where $n$ is the number we want to reverse.

After all you just have to compute $r(n)$ for a given $n$, and if $r(n)=n$ then $n$ was palindromic.

Of course there are much more easier solutions using some computer science.

user153012
  • 12,240
  • This pretty much answers the question thank you , but do you know were a i could find the full demonstration that leads to this formula? (A book source would be nice) i cant seem to find much on this. – Amr El Aswar Mar 07 '15 at 13:18