1

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$?

Zach K
  • 187
  • Also see https://math.stackexchange.com/questions/3654663/is-there-a-function-that-can-produce-unique-solutions-for-r-given-l-t-a – Zach K May 02 '20 at 06:02

1 Answers1

2

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.

  • Aston Villa? ${}{}$ – copper.hat May 02 '20 at 04:30
  • 1
    Yes, that is correct! – Sarvesh Ravichandran Iyer May 02 '20 at 04:32
  • You did answer my posted question... though I was not expecting the function to not have unique solutions. Do you know of a function that does have a unique solutions and accurately represents the situation or would this be better suited for a separate question? – Zach K May 02 '20 at 04:52
  • I am not sure that such a function can be found, if I were asked. My question to you would be : why do you want a unique solution? Also, thanks for the acceptance. – Sarvesh Ravichandran Iyer May 02 '20 at 05:00
  • I was looking to see if there was a function that yields a unique solution to be able to quickly and accurately print a long string of text to a console over multiple lines with a certain number of tabs. I do have other working solutions which involve programming loops instead of a pure mathematical equation, though I wanted to see if it was possible to do it faster otherwise. I'll take your word for it though and use a different solution. – Zach K May 02 '20 at 05:07
  • I see. I like your question, but I am not sure I can contribute(somebody better than me will). You may post it as a separate question, I am sure you will get good suggestions. – Sarvesh Ravichandran Iyer May 02 '20 at 05:09