Number reverse formula reverses the number - function argument. f(123) = 321
Let analyze how 123 converted to 321.
321 = 1*1 + 2*10 + 3*100
So we need a function that will get an nth digit from the number x.
\(p(x,n)={\left\lfloor\frac{x}{10^{n}}\right\rfloor}\operatorname{mod}10\)
\(\left\lfloor\frac{x}{10^n}\right\rfloor\) can be written as \(\lfloor10^{-n}x\rfloor\)
\(\left\lfloor\frac{x}{10^n}\right\rfloor=\lfloor10^{-n}x\rfloor\)
So
\(p(x,n)={\lfloor10^{-n}x\rfloor}\operatorname{mod}10\)
The other function we need is the function that gets number length.
\(l(x) = \lfloor\log_{10}x\rfloor\)
Substitute these formulas instead of numbers:
\(rev(123) = p(123, 0)*10^{l(123)-0} + p(123, 1)*10^{l(123)-1} + p(123, 2)*10^{l(123)-2} = 3*100 + 2*10 + 1*1 = 321 \)
So, here's completed function:
\(rev(x)=\sum^{l(x)}_{n=0}p(x,n)*10^{l(x)-n}\)
Where x is a natural number or 0.
All in heap:
\(rev(x)=\sum^{\lfloor\operatorname{log}_{10}x\rfloor}_{n=0}\left\lfloor10^{-n}x\right\rfloor\operatorname{mod}10*10^{\lfloor{log_{10}x\rfloor}-n}\)
Look at this in Desmos: https://www.desmos.com/calculator/8ilh6dzqcp
Here is a hastily written one line code in C++ that does the same thing in 1 nano second:
a=123; b=int(log(a));c=int(a / 10^b);d=int(a / 10^(b-1)) %10; e=a%10; f=e*(10^b) + d*(10^(b-1)) + c*(10^(b-2))=321
I also can write the program that reverses the given number but my main target was to write the number reverser using math.