0% found this document useful (0 votes)
225 views

A Guessing Game PDF

The document describes a guessing game where the computer selects a random integer between 1 and 16. It explains that one approach is linear search, where the player guesses numbers sequentially from 1 to 16, requiring on average 8 guesses. A more efficient approach is binary search, where the player guesses the midpoint number first and eliminates half the remaining possibilities with each guess, allowing the number to be found in at most 4 guesses on average. The document then has the player try guessing a number between 1 and 300 using binary search, which should require no more than 9 guesses, and asks them to explain why mathematically.

Uploaded by

Livia Bujor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

A Guessing Game PDF

The document describes a guessing game where the computer selects a random integer between 1 and 16. It explains that one approach is linear search, where the player guesses numbers sequentially from 1 to 16, requiring on average 8 guesses. A more efficient approach is binary search, where the player guesses the midpoint number first and eliminates half the remaining possibilities with each guess, allowing the number to be found in at most 4 guesses on average. The document then has the player try guessing a number between 1 and 300 using binary search, which should require no more than 9 guesses, and asks them to explain why mathematically.

Uploaded by

Livia Bujor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

A guessing game

Let's play a little game to give you an idea of how different algorithms for
the same problem can have wildly different efficiencies. The computer is
going to randomly select an integer from 1 to 16. You'll keep guessing
numbers until you find the computer's number, and the computer will
tell you each time if your guess was too high or too low:

https://mybinder.org/v2/gh/emefer/Guessing-Game.git/master?filepath=guessing_game_1_10.ipynb

Once you've found the number, reflect on what technique you used when
deciding what number to guess next.

Maybe you guessed 1, then 2, then 3, then 4, and so on, until you guessed
the right number. We call this approach linear search, because you
guess all the numbers as if they were lined up in a row. It would work.
But what is the highest number of guesses you could need? If the
computer selects 16, you would need 16 guesses. Then again, you could
be really lucky, which would be when the computer selects 1 and you get
the number on your first guess. How about on average? If the computer
is equally likely to select any number from 1 to 16, then on average you'll
need 8 guesses.

But you could do something more efficient than just guessing 1, 2, 3, 4, …,


right? Since the computer tells you whether a guess is too low, too high,
or correct, you can start off by guessing 8. If the number that the
computer selected is less than 8, then because you know that 8 is too
high, you can eliminate all the numbers from 8 to 16 from further
consideration. If the number selected by the computer is greater than 8,
then you can eliminate 1 through 7. Either way, you can eliminate about
half the numbers. On your next guess, eliminate half of the remaining
numbers. Keep going, always eliminating half of the remaining numbers.
We call this halving approach binary search, and no matter which
number from 1 to 16 the computer has selected, you should be able to
find the number in at most 4 guesses with this technique.

Here, try it for a number from 1 to 300. You should need no more than 9
guesses.

https://mybinder.org/v2/gh/emefer/Guessing-Game.git/master?filepath=guessing_game_1_300.ipynb

How many guesses did it take you to find the number this time? Why
should you never need more than 9 guesses? (Can you think of a
mathematical explanation)?

We'll return to binary search, and we'll see how you can use it to
efficiently search for an item in a JavaScript array. But first, let's look at
an algorithm for a trickier problem.

This content is a collaboration of Dartmouth Computer


Science professors Thomas Cormen and Devin Balkcom, plus the Khan
Academy computing curriculum team. The content is licensed CC-BY-NC-
SA.

You might also like