How can I extract the value of a variable using simple algebra when the max function is involved?

51 Views Asked by At

I have a C program that does the following calculation:

int b = 16;    
int a = max(1, ( (b + 3) / 4 ) ) * max(1, ( (b + 3) / 4 ) ) * 16;

More easily read as: $$ a = \max\left(1, \frac{b + 3}{4}\right)^2 × 16 $$

I would like to know how I could use simple algebra in order to find what is the equivalent algorithm to find the value of b if I am only given the value of a? For example:

int a = 256;
int b = ?
2

There are 2 best solutions below

5
On BEST ANSWER

To say it another way, the range of the function on the RHS is $a \ge 16$ (plot $a$ against $b$ if you don't see it.) There are three cases:

  • if $a \lt 16$, it is not in the range, so there is no $b$ that can produce that value.

  • if $a = 16$, then $\max(1, (b+3)/4) = 1$ and any $b$ where $(b+3)/4 \le 1$ will do.

  • if $a \gt 16$ then there is a unique $b = 4\sqrt{a/16} - 3 = \sqrt{a} - 3$ that produces that $a$ (assuming you want the positive square root only).

3
On

No, you cannot. This is because there exists more than one possible value for $b$ that returns the same value of $a$. For example,

$$\max\left(1, \frac{-1+3}{4}\right)\cdot 16=\max\left(1, \frac{-2+3}{4}\right)\cdot 16=16.$$

So, if all you know is that $\max\left(1, \frac{b+3}{4}\right) = 1$, you have no way of knowing what $b$ is, since it could be $-1$ or $-2$ or some other number.


However, there is still something you can extract, if you are given $a$:

If $a=16$:

In this case, you know that $\max\left(1, \frac{b+3}{4}\right)=1$, and this happens if and only if $\frac{b+3}{4}\leq 1$ (since, if it were greater than one, then $1$ would not be the maximum). So, in this case, you can conclude (after multiplying the inequality by $4$) that $b\leq 1$.

If $a\neq 16$:

In this case, you know for a fact that $16\cdot \frac{b+3}{4} = a$, since $a$ can be either $16$ or $16\cdot \frac{b+3}{4}$, and it isn't $16$. Therefore, you have $4b+3=a$ and you can, in this case, calculate exactly what $b$ is: $b=\frac{a-3}{4}$