Binary search game

Find a number from 1 to 1,000 in 10 steps. That is binary search, and you can try it right here.

Range
Today's challenge
Range1–1,000
Attempts1 per day
Same numberfor everyone
New number in--:--:--

Guesses 0
or press Enter
Your guesses
Daily challenge
SOLVED!

Your name, guesses and time go on today's public board.

Daily challenge
Rankings
    Next challenge in
    --:--:--
    Round complete
    SOLVED!

    The idea

    What is binary search?

    Binary search finds a value in a sorted list by checking the middle, then throwing away the half that cannot contain it. Repeat until one value is left. Guess the number is the same idea: the numbers 1 to 1,000 are already in order, and each bigger or smaller hint tells you which half to keep.

    Speed

    Why it is so fast

    Every step halves what is left, so the number of steps grows very slowly. The steps needed are the smallest n where 2 to the power of n is larger than the range:

    1–10
    4
    guesses max
    1–100
    7
    guesses max
    1–1,000
    10
    guesses max
    1–10,000
    14
    guesses max

    A million numbers take only 20 guesses, because 2 to the power of 20 is 1,048,576.

    Code

    Binary search in code

    This is the same strategy the game rewards, written in Python:

    def guess_number(low, high, answer):
        guesses = 0
        while low <= high:
            guess = (low + high) // 2
            guesses += 1
            reply = answer(guess)  # "bigger", "smaller" or "correct"
            if reply == "correct":
                return guess, guesses
            if reply == "bigger":
                low = guess + 1
            else:
                high = guess - 1

    Teachers

    Use it in class

    Play one round on the board with students calling out guesses and watch the bar narrow. Ask how many guesses 1 to 1,000,000 would need. Then let pairs play Duo mode.

    More ways to play

    Pick your game

    FAQ

    Questions

    How many steps does binary search take?

    At most the number of times you can halve the range before one value is left, about log2 of the range rounded up. 1,000 items need 10 steps and a million need 20.

    Why does binary search need sorted data?

    Each comparison must tell you which half is impossible. With the numbers 1 to 1,000 the order is built in, which is why this game is a perfect demo.

    Is guess the number a good way to teach binary search?

    Yes. Students feel the halving after a couple of rounds, and the range bar shows each step.