Home > Blockchain >  find 2 numbers that multiply to given number using a given ratio
find 2 numbers that multiply to given number using a given ratio

Time:06-24

I am trying to make a nested for loop where the outer loop and the inner loop do a certain amount of total iterations, but they do not have the same amount of iterations each. Instead, the iterations for each loop should be calculated based on a given ratio and a given total iterations. My function will take in a total amount of iterations, (M), and a ratio. Based on this, it should calculate how many times the outer loop should iterate and how many times the inner loop should iterate, so that the total iterations equals M.

For example, given ratio = 0.5 and M = 225, each loop should iterate the same amount of times (since the ratio is 50%), and the total iterations should equal 225. In this case, each loop should do 15 iterations:

for(int i = 0; i < 15; i  ) {
    for(int j = 0; j < 15; j  ) {
    ...etc

In the context of some code:

//these will be given as function parameters, but for these purposes they are constant
int M = 225;
double ratio = 0.5;

int outerIterations = //some calculation
int innerIterations = //some other calculation

//the amount of iterations each loop has done
int totalIters = 0;

for(int i = 0; i < outerIterations; i  ) {
    for(int j = 0; j < innerIterations; j  ) {
        totalIters  ;
        //in here, something is done M times
    }
}

In the above example, totalIters should equal 200, outerIterations should equal 15, and innerIterations should also equal 15. This example is very easy, because it is just 50/50 between the two loops (because the ratio is 0.5). The problem is I dont know how to get the 2 values I need from the ratio and the total iterations.

I have tried:

int innerIterations = (int)(M * ratio)
int outerIterations = (int)(M / innerIterations)
//This produces 2 numbers that will multiply to M, but doesn't maintain the correct ratio

other than that I have no clue where to begin, because it seems this is a fairly unique problem and none of the google/stackoverflow searches i have done have produced anything relevant.

Overall, the question I'm asking is what calculations I need to perform in order to get these 2 values (outerIterations and innerIterations), so the the for loops maintain the ratio given, and iterate M times.

CodePudding user response:

First, we need to establish what your ratio means. I'm going to take it to mean the ratio of the outer iterations to the total (outer inner).

outer/(outer   inner) = ratio
outer = ratio(outer   inner)
outer = ratio * outer   ratio * inner
outer - ratio * outer = ratio * inner
outer(1 - ratio) = ratio * inner
outer = ratio/(1 - ratio) * inner

We also know the product of outer and inner will be the total iterations

outer * inner = total
ratio/(1 - ratio) * inner² = total
inner² = total(1 - ratio)/ratio
inner = √(total(1 - ratio)/ratio)

outer * inner = total
outer = total/inner

So, that leaves us with the formulas:

1) inner = √(total(1 - ratio)/ratio)
2) outer = total/inner

In your case:

inner = √(225(1 - 0.5)/0.5) = √(225) = 15
outer = 225/15 = 15

You'll need to decide how to handle the cases when there isn't an integer solution.

  • Related