WoodCentral Forums

Knowledge You Can Build On℠ — Est. 1998

Guess a number between 1 and x

Posts

Guess a number between 1 and x

#1

Peter Martin

There is a game in which someone thinks of a number between 1 and x, and you have to guess the number. They respond with either "too low," "too high," or "correct."

A strategy is to keep guessing halfway between the lowest and highest known guesses.

This is called a binary search, and it is the optimal strategy when you receive a reliable "too high" or "too low" response. You continually eliminate half of the remaining possibilities with each guess.

It is surprisingly efficient, where one can find the answer in a range of 1 to 1000 with only 10 guesses.

How many guesses would it take to get the answer in a range from 1 to a million? A billion? A trillion?

Try it here :)

Re: Guess a number between 1 and x

#2

I get 20 guesses for a million, 30 for a billion, and 40 for a trillion. The useful off-by-one detail is that k guesses can cover 2^k - 1 numbers: the middle guess can be correct itself, and each side then has room for up to 2^(k-1) - 1 more. For a quick sanity check, two guesses can cover 1 through 3, but not 1 through 4.