If I tell you:
"I think of a number from 0 to n, and I will tell you if your assumption is high or low," then you will immediately reach the binary search.
What if i delete the upper border? those. I think of positive integers, and you need to guess it.
One possible way is to guess 2, 4, 8, ... until you guess 2 ** k for some k, and I say "below." Then you can apply binary search.
Is there a faster method?
EDIT:
It is clear that any decision will take time proportional to the size of the target number. If I take the Graham number through the Ackerman function, we will wait until what strategy you pursue.
I could also suggest this algorithm: guess each integer in turn, starting at 1.
It is guaranteed to end in a finite time, but still it is clearly much worse than my "force 2" strategy. If I can find the worst algorithm (and I know that it is worse), then maybe I can find the best?
For example, instead of degrees 2, perhaps I can use powers of 10. Then I find the upper bound in steps of log_10(n) instead of steps of log_2(n) . But I have to look for more space. Say k = ceil(log_10(n)) . Then I need the log_2(10**k - 10**(k-1)) steps for my binary search, which I think is about 10+log_2(k) . For credentials 2, I have roughly log_2(log_2(n)) steps for my search phase. What is the gain?
What if I look up using n**n ? Or some other sequence? Does it win for those who can find the sequence that grows fastest? Is this a problem with the answer?
Thanks for your thoughts. And my apologies to those you offer, I start with MAX_INT or 2 ** 32-1, as I am clearly shying away from the limits of practicality here.
COMPLETION:
Hello to all,
Thank you for your responses. I accepted the answer of Norman Ramsey (and the commentator one by one) for what I understood as the following argument: for the target number n, any strategy should be able to distinguish (at least) the numbers from 0..n, which means you need (by at least) O (log (n)) comparison.
However, you also pointed out that the problem is not defined in the first place, since it is impossible to choose a “random positive integer” with a uniform probability distribution (or, rather, a uniform probability distribution cannot exist over an infinite set). And as soon as I give you an uneven distribution, you can split it in half and apply the binary search as usual.
This is a problem that I often thought about when I get around, so I'm glad that I have two convincing answers.