# Algorithm for iteratively distributing $5$ units to two variables

Please point me to the right place if this isn't the right one to ask in.

• There are 2 initial integer variables (the values of the two can be changed). $$a = 4$$ and $$b = 1$$
• There's a function. Each run of the function can distribute a total of $$5$$ to $$a$$ and $$b$$.
• So any positive integer combination of $$5$$ is possible.
• So a run for example could be $$a = a + 1$$ and $$b = b + 4$$.
• Another run could be $$a = a + 3$$ and $$b = b + 2$$.
• Another run could be $$b = b + 5$$.
• Also, the function increases $$a$$ by $$1$$ at the end of each iteration. (So at each run, $$a$$ will increase by $$1$$).

What is the lowest number of runs that could be done such that $$a \geq 99$$ and $$b \geq 99$$?

Here's sample code where I did a super simple inefficient approach that's also probably not correct/doesn't give the lowest number possible:

public static void Main()
{
int core = 4;
int shell = 1;
int counter = 1;
while (core < 99 || shell < 99)
{
++core;
for (int i = 0; i < 5; i++)
{
if ((shell + 1) <= 99)
{
shell++;
}
else
{
core++;
}
}
Console.WriteLine($"counter: {counter} | core: {core} | shell: {shell}"); ++counter; } } //////////// output of above: counter: 1 | core: 5 | shell: 6 counter: 2 | core: 6 | shell: 11 counter: 3 | core: 7 | shell: 16 counter: 4 | core: 8 | shell: 21 counter: 5 | core: 9 | shell: 26 counter: 6 | core: 10 | shell: 31 counter: 7 | core: 11 | shell: 36 counter: 8 | core: 12 | shell: 41 counter: 9 | core: 13 | shell: 46 counter: 10 | core: 14 | shell: 51 counter: 11 | core: 15 | shell: 56 counter: 12 | core: 16 | shell: 61 counter: 13 | core: 17 | shell: 66 counter: 14 | core: 18 | shell: 71 counter: 15 | core: 19 | shell: 76 counter: 16 | core: 20 | shell: 81 counter: 17 | core: 21 | shell: 86 counter: 18 | core: 22 | shell: 91 counter: 19 | core: 23 | shell: 96 counter: 20 | core: 26 | shell: 99 counter: 21 | core: 32 | shell: 99 counter: 22 | core: 38 | shell: 99 counter: 23 | core: 44 | shell: 99 counter: 24 | core: 50 | shell: 99 counter: 25 | core: 56 | shell: 99 counter: 26 | core: 62 | shell: 99 counter: 27 | core: 68 | shell: 99 counter: 28 | core: 74 | shell: 99 counter: 29 | core: 80 | shell: 99 counter: 30 | core: 86 | shell: 99 counter: 31 | core: 92 | shell: 99 counter: 32 | core: 98 | shell: 99 counter: 33 | core: 104 | shell: 99  • I encourage you to edit your post to provide a more descriptive title. – D.W. Sep 10, 2023 at 7:48 • 1. Please specify whether a run can distribute 0 to a and 5 to b, or vice versa. 2. Please explain what you mean by "at the end". At the end of each run (once per run)? After performing all runs (once overall)? – D.W. Sep 10, 2023 at 7:49 • the sixth point, b = b + 5 covers that. you can give 0 to any Sep 10, 2023 at 9:57 • also added sample code with print variables after each run to demonstrate what I mean Sep 10, 2023 at 10:08 ## 1 Answer Since you add $$1$$ to $$a$$ at each iteration, while $$b$$ can only be increased summing a portion of $$5$$ at each iterations, the idea is that of bringing $$b$$ to $$99$$ and then take care of $$a$$. This is an outline for the algorithm: 1. while $$b<95$$ add $$5$$ to $$b$$. As a consequence, you will add $$1$$ to $$a$$ 2. if $$b<99$$ (so it would be between $$95$$ and $$98$$ add to $$b$$ the remaining quantity in order to get $$b=99$$. Add the remaining part to $$a$$ 3. while $$a<99$$ add $$5$$ to $$a$$ ($$+1$$ at the end of the run$

This algorithm provide an optimal solution. In your case where $$a=4$$ and $$b=1$$:

1. Step 1 executes $$\lfloor \frac{98}{5}\rfloor=19$$ runs in order to get to $$b=96$$. As a consequence, $$a$$ is increased by $$19$$. So, at the end of first step we have done $$19$$ runs and we have $$a=23$$, $$b=96$$.
2. Step 2 executes one run, adding $$3$$ to $$b$$ and $$2$$ to $$a$$. At the end of the run $$a$$ is increased by $$1$$. So, at the end of second step we have done $$20$$ runs and we have $$a=26$$, $$b=99$$.
3. Step 3 executes $$\lceil\frac{99-26}{6}\rceil=\lceil\frac{73}{6}\rceil=13$$ runs. At the end of third step we have done $$33$$ runs and we have $$a\geq 99$$ and $$b=99$$.

So 33 is the minimum number of runs.

• I edited my question with sample code, got answer of 33, but i think my way is not good, there should be a much better way. Sep 10, 2023 at 10:06
• @whatdafq of course you get $33$, you are adding $6$ at each iterations: at first run you add $1$ to core and $5$ to shell, same in second run. Sep 10, 2023 at 10:09
• that's the last point (i've edited, added more explanation): - Also, the function increases a by 1 at the end no matter what independently of the distribution. Sep 10, 2023 at 10:10
• @whatdafq the text was not clear to me. Now I think I understood and I edited my solution. Check it Sep 10, 2023 at 10:25
• Thanks. How about if b has initial value of 4, or 7, or maybe even change a? Will the same logic work? Sep 10, 2023 at 11:14