Why does this expression for finding maximum of 2 positive integers work, being not symmetric with respect to variables interchange?

34 Views Asked by At

My father just gave this solution for finding the maximum of 2 positive integer variables.

$$ \max(a, b) = a * \lfloor\frac{a - 1}{b}\rfloor^{\lfloor\frac{b} {a}\rfloor} + b * \lfloor\frac{b} {a}\rfloor^{\lfloor\frac{a} {b}\rfloor}$$

It is killing me as I thought that any such expression should be symmetric with respect to variables interchange.

Update: abs() was not allowed to use in the solution.

2

There are 2 best solutions below

3
On

If $0<a<b$, we have $\lfloor \frac{a-1}b\rfloor =0$, $\lfloor \frac{a}b\rfloor =0$, $\lfloor \frac ba\rfloor \ge 1$, and so the expression computes $$ a\cdot0^{(\ge1)}+b\cdot (\ge1)^0=a\cdot 0+b\cdot 1=b.$$

If $0<b<a$, we have $\lfloor \frac{a-1}b\rfloor \ge1$, $\lfloor \frac{a}b\rfloor \ge1$, $\lfloor \frac ba\rfloor =0$, and so the expression computes $$ a\cdot(\ge1)^{0}+b\cdot (0)^{(\ge1)}=a\cdot 1+b\cdot 0=a.$$

Finally, you will find that $a=b$ will lead to $$a\cdot 0+b\cdot 1, $$ which is correct, but asymmetric. With symmetric expressions similar to the given, you might end up with either $a+a$ or $0+0$, both wrong. So the symmetry had to be broken.

By the way, a simpler and more symmetric expression would be $$\max\{a,b\}=\frac{a+b+|a-b|}{2}.$$

2
On

Your expression is asymmetrical to make it come out right when $a=b$. So in the spirit of the original, I offer this symmetrical expression:

$$\max(a, b) = a \cdot \left\lfloor\frac{a}{b}\right\rfloor^{\left\lfloor\frac{b} {a}\right\rfloor} + b \cdot \left\lfloor\frac{b} {a}\right\rfloor^{\left\lfloor\frac{a} {b}\right\rfloor} - \frac12(a+b)\left\lfloor\frac{a}{b}\right\rfloor\cdot\left\lfloor\frac{b}{a}\right\rfloor$$