How do you solve $r=\operatorname{ceil}\left(\frac{l+r\cdot t}{c}\right)$ for $r$?

25 Views Asked by At

As I was creating a function for printing monospaced text to a limited-length console with a certain level of indentation, I found a difficult equation to solve without programming around it: $r=\operatorname{ceil}\left(\frac{l+r\cdot t}{c}\right)$. This function represents the amount of rows $r$ in a console a string of text with length $l$ takes up if each line of text is prefixed by $t$ spaces and the maximum length of the console is $c$. For example, this text:

    oooooooooooooo
    oooooooooooooo
    ooooo

satisfies the equation: $r=\operatorname{ceil}\left(\frac{34+r\cdot4}{19}\right)$ for $r=3$.

My question is simple; how do you solve this equation for $r$?

1

There are 1 best solutions below

6
On BEST ANSWER

Solve instead : $$ r-1 < \frac{l+rt}{c} \\ r \geq \frac{l+rt}{c} $$

which are a system of inequations equivalent to the equation $r = \mbox{ceil}(\frac{l+rt}{c})$

Upon solving you get : $$ rc-c < l+rt \implies r(c-t) < l+c \implies r < \frac{l+c}{c-t} \\ rc \geq l+rt \implies r(c-t) \geq l \implies r \geq \frac{l}{c-t} $$

Therefore, the answer is $\mathbb Z \cap \left[\frac{l}{c-t} , \frac{l+c}{c-t}\right)$.

In the above example, $l = 34, t = 4 , c= 19$. This gives the range $\left[\frac{34}{15}, \frac{53}{15}\right)$. The only integer in this range is $3$.

However, suppose I took $l = 34, t = 10 , c = 19$, then the above range is $\frac{34}{9} \to \frac{53}{9}$, which has $r = 4$ and $r=5$ as solutions. So uniqueness is not guaranteed.