T
T
Torento203452021-02-25 15:52:36
Algorithms
Torento20345, 2021-02-25 15:52:36

What is the formula to calculate the minimum number of times to set the range step for calculation?

How can I find out the recommended range step for calculation?
For example, we have the number 128, which we do not know, and when checking, we will get the value that the result is more or less than 128, well, or we guessed it.

The maximum length of the array is 256.

How can we find out the recommended step to do the search step by step until we get the result that the number was greater, and then we will add the +1 request to the previous step until we get 128.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-02-25
@Torento20345

Sounds like a binary search. The initial step is half the length of the array (128).
Next, half of that, and so on. So there will be a maximum of 8 checks in total.
If it is necessary to make sure +1 in the second step, then you can calculate the maximum number of checks if the first step is X, and the length of the array is N. The maximum number of
first checks will be - floor(N/X), the second (with step +1) - X -one.
You can roughly calculate in real numbers and try to minimize f(x) = N/X+X.
You can find the derivative f'(x) = 1-n/x^2. Zero for this derivative at x=sqrt(n).
From here it turns out that approximately sqrt(256)=16 is the desired length. The maximum number of checks is 32.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question