Is there an algebraic method to concat two numbers?

1.2k Views Asked by At

I'm searching an algebraic way to concat numbers in base $10$.
Concatening two numbers is to put side by side their notations.

Let $c$ a concatenating function.

$c(2,2) = 22$

$c(8,9) = 89$

$c(11,0) = 110$

$c(14856,183) = 14856183$

I know we can easily do that using programmation, but it would be so elegant to do it with Maths. :)

I think that if $n(x)$ return the number of digits in a number $x$,
$c(a,b) = a * 10 ^{n(b)} + b $

But I don't know how to do it. Perhaps you can help ?

2

There are 2 best solutions below

3
On BEST ANSWER

Assuming that all of the numbers are non-negative integers in base $10$, then the problem peterwhy describes can be fixed. We just need a special function to detect if $b$ is $0$.

A general trick to detect strict inequalities like $a>b$ versus $b>a$ is to involve the absolute value of the difference. In our case, $b>1/2$ if $b\ne0$, and $b<1/2$ if $b=0$. Therefore, we should consider the quantity $\left|b-\frac12\right|$. It equals $b-\frac12$ unless $b=0$. To make it easier to detect what's going on and avoid fractions, consider $\left|b-\frac12\right|-\left(b-\frac12\right)$. That's $0$ unless $b=0$ in which case it's $\left|-\frac12\right|-\left(-\frac12\right)=1$. This is perfect for us, because we need to add $1$ to the power of $10$ exactly when $b=0$.

Therefore, our final formula is $$c(a,b)=b+a*10^{\left\lfloor\log_{10}\left(b+1\right)\right\rfloor+\left|b+1/2\right|-\left(b-1/2\right)}$$

(Note: If you want something that looks weirder, you could also use $\left\lceil \left|b-\frac12\right|\right\rceil-b$ in place of the $\left|b-\frac12\right|-\left(b-\frac12\right)$ part.)

1
On

Thanks to @egreg , @peterwhy and @xaviermos2 I can now say that $c$ is concatening function for $a$ and $b$ natural numbers !

$c(a,b) = a*10^{⌈log10(b+1)⌉}+b$