# Optimal algorithm to guess any random integer without limits?

### Guessing Game In Range $$[1, n]$$

The classical guessing game goes something like this...

1. Our friend thinks of an integer between $$1$$ and $$100$$ (let's say they pick $$42$$).

2. We try to guess that number with the fewest guesses possible: $$100, 3, 7, 30, ...$$

3. But every time we guess, they say whether the actual number is higher or lower than what we guessed... $$100(lower), 3(higher), 7(lower), 30(lower), ...$$

There is actually an optimal solution to this problem. By knowing whether their number is higher or lower than our previous guess, we can always choose our next guess in exactly the middle of the remaining range... $$\begin{array}{|c|c|c|c|c|} \hline \text{#} & \text{Options Range} & \text{Number of Options} & \text{Guess} & \text{42 Is Higher Or Lower} \\ \hline 1. & 1...100 & 100 & 50 & \text{Lower} \\ \hline 2. & 1...49 & 49 & 25 & \text{Higher} \\ \hline 3. & 26...49 & 24 & 37 & \text{Higher} \\ \hline 4. & 38...49 & 12 & 43 & \text{Lower} \\ \hline 5. & 38...42 & 5 & 40 & \text{Higher} \\ \hline 6. & 41...42 & 2 & 41 & \text{Higher} \\ \hline 7. & 42...42 & 1 & 42 & \text{Win} \\ \hline \end{array}$$

By using this method we are reducing the number of remaining options by roughly a half with each guess. It took us only $$7$$ guesses to guess a random number from $$1$$ to $$100$$.

### Infinite Guessing Game $$[1, \infty)$$

What if we ask our friend to pick an integer $$k$$ between $$1$$ and infinity?

Is there an algorithm which can be proven to be the most efficient in terms of the average number of guesses for finding the answer?

Can it be proven that there can not exist such an algorithm?

My guess is that we need to solve this problem in two separate steps:

1. Find an upper-bound $$n$$ that is higher than $$k$$.

2. Solve the original problem in the range $$[1,n]$$.

If we had all the time in the universe, and because the number $$k$$ is a finite number, we will guess it eventually.

We can even define some possible ways of finding $$n$$:

• Counting from 1 upwards. $$1, 2, 3, 4, ...$$ (Takes exactly $$k$$ guesses to reach the answer.)
• Guessing a random number in the sub-range of possibilities. (How to define selecting a random number between $$1$$ and infinity?)
• Squaring our guess until we find an upper bound. $$10, 100, 10^4, 10^8, 10^{16}, ...$$
• Cubing our guess until we find an upper bound? $$10, 1000, 10^9, 10^{27}, ...$$ (What if we raise to an even higher power?)
• Exponentially increasing our guess until we find an upper bound. $$10, 2^{10}, 2^{2^{10}}, ...$$

We could keep defining different techniques like these forever.

The questions are:

1. Are all these techniques equally valid?
2. If not, how can they be ordered in terms of efficiency?
3. If the number $$k$$ can be anywhere between $$1$$ and $$k$$, does this automatically mean that the average number of guesses for any technique tends to infinity?
4. The problem is not only finding the upper-bound as quickly as possible, but also finding it in a way that after we have found it we can find the number itself as quickly as possible.

### Double-Infinite Guessing Game $$(-\infty, \infty)$$

You know the question to this...

• What is the distribution of the picked numbers? – Michael Biro Mar 27 at 20:57
• You indeed cannot talk about "average number of guesses" without specifying a distribution on the choice of $k$. "Uniform" is not going to cut it here.... – Clement C. Mar 27 at 20:59
• As pointed out in the above comments, you need to specify the distribution of the input to make your question precise. However, your list of possible algorithms omits the one that comes first to my mind for the $[1, \infty)$ case: namely, double your initial guess until you've got an upper bound. This will give an algorithm that has logarithmic complexity relative to the input $k$. I think your suggestions will all be much worse than this. – Rob Arthan Mar 27 at 21:13

A practical approach to this is to start with $$n=1$$ (or $$n=$$ any positive integer you like), and double your guess until you reach an $$n\ge k$$. Then you can conduct your binary search on the remaining possibilities $$n/2 < k < n$$.
This can't be proven to be the most efficient method, because to do that you would need to know the probability of each $$k$$, which presumably you don't. But it is an approach I have used in more than one computer programming problem.
By the way, your Doubly Infinite Guessing Game can be reduced to the Singly Infinite version, simply by guessing $$n=0$$ at your first turn.
• This is actually a good strategy even without distribution on the input: you can prove a related lower bound on the number of guesses in the worst-case setting (as a function of $k$ itself). Namely, you will have $O(\log k)$ number of guesses, and you need $\Omega(\log k)$ anyways (even if I give you an upper bound on $k$, say a value $n$ such that $n/100 \leq k \leq 100n$.) – Clement C. Mar 27 at 21:30
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 34, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8566089868545532, "perplexity": 280.1858607121749}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-43/segments/1570986675598.53/warc/CC-MAIN-20191017172920-20191017200420-00396.warc.gz"}