-1

Say we know a "limit" value A and an "initial increment" B as well as a "target increment" C where C < B << A

Unfortunately I lack the skill to express this mathematically as this comes from a programming task, but hope this makes sense:

Say we continuously loop the following steps until A <= 0:

  1. we decrease A by B.
  2. We decrease B in such a way that when this loop ends (aka A <= 0), B is exactly C.

Effectively we decrease A at a steadily slower rate but just fast enough that we do not become slower more than that given minimum B.

Now how do we find by how much to decrease B every step and how many steps will it take? (the one can easily be calculated from the other of course).

Please excuse the sloppy definitions. Am coming to this from a programming problem (wanting to fit a series of visually appealing curves on a line).

Huge thanks in advance!

  • Assuming $C>0$, what if we decrease $B$ to $C$ anyway in the first iteration? – peterwhy Jul 15 '22 at 01:34
  • Since B is way smaller than A, there is no way that A can be brought down to 0 in a single iteration. The constraint is that both happens in the same number of steps: A reaches 0 and B reaches C. – DragonGamer Jul 15 '22 at 01:41
  • But this satisfies 2. that, when this loop ends (aka $A\le 0$) (possible because $C>0$), $B$ is exactly $C$. – peterwhy Jul 15 '22 at 01:45
  • Ah, A, B and C are known! For example A is 100, B is 20 and C is 5. I'm looking for a solution that works with all reasonable values... – DragonGamer Jul 15 '22 at 01:55
  • Questions: (1) Are values all positive integers? (2) Can we subtract the same value of $B$ more than once before (eventually) decrementing $B$? – paw88789 Jul 15 '22 at 02:51

1 Answers1

1

While not explicitly specified in the question, I am looking for a decrease pattern such that $B$ decreases linearly every iteration. (Alternatively, for example $B$ can be decreased to $C$ anyway in iteration $0$, and still satisfy the given goal)

Let $T$ be the number of steps (to be decided). Let $d$ be the decrease of $B$ per iteration:

$$d = \frac{B_0-C}T$$

The values of $A$ and $B$ at the end of iteration $i-1$, or the respective values at the start of iteration $i$, will be:

$$\begin{align*} B_i &= B_{i-1} -d\\ &= B_0 - id\\ A_i &= A_{i-1} - B_{i-1}\\ &= A_0 - B_0 - B_1 - \cdots - B_{i-1}\\ &= A_0 - iB_0 + 0d + 1d + \cdots + (i-1)d\\ &= A_0 - iB_0 + \frac{(i-1)id}{2} \end{align*}$$

The goal is to have non-positive $A$ at the end of iteration $T-1$:

$$\begin{align*} A_T &\le 0\\ A_0 - TB_0 + \frac{(T-1)Td}{2} &\le 0\\ A_0 - TB_0 + \frac{(T-1)(B_0-C)}{2} &\le 0\\ A_0 + \frac{-TB_0 - TC-B_0+C}2 &\le 0\\ 2A_0 -B_0+C &\le T(B_0+C)\\ T &\ge \frac{2A_0-B_0+C}{B_0+C} \end{align*}$$

So try $d$ depending on $T=\frac{2A_0-B_0+C}{B_0+C}$ (which may be a non-integer).

  • Larger $T$ means $A$ is more likely to become non-positive before $T$ iterations, and before $B$ reaches $C$.
  • Smaller $T$ means $A$ is more likely to stay positive after $T$ iterations, and after $B$ reaches $C$.

For example, $A_0=100, B_0 = 20, C = 5$, then

$$\begin{align*} T &= \frac{2\cdot100-20+5}{20+5} = 7.4\\ d &= \frac{20-5}{7.4} = 2.027 \end{align*}$$

$$\begin{array}{c|ll} i\ \text{(iteration)}&A_i\ \text{(starting value)}&B_i\ \text{(starting value)}\\\hline 0&100&20\\ 1&80.000&17.973\\ 2&62.027&15.946\\ 3&46.081&13.919\\ 4&32.162&11.892\\ 5&20.270&9.865\\ 6&10.405&7.838\\ 7&2.568&5.811\\ 8&-3.243&3.784 \end{array}$$

Note how $B_8$ is below $C=5$, but that value is not used for later $A$ and hopefully is unimportant.

peterwhy
  • 22,256
  • Oh wow, I had already begun implementing an iterative approach to find plausible values. Your solution works very well! Thank you kindly. In case you are curious, this all was needed just so the "humps" of this procedurally generated "thought bubble" aka cloud, are decrementing smoothly no matter the overall size (larger bubble = more humps): https://www.dropbox.com/s/yhjmxvvm5ky2tyx/Thought%20Bubble.png?dl=0 – DragonGamer Jul 16 '22 at 00:34