Let's think of a number...
This is all going to get a little bit puzzling. That's a good thing.
There are some ways we can extend this idea a bit further as well.
There's another way to find a number. And the algorithm is simpler as well.
We can code this as well. But, even better, we can code it so that the computer does the guessing. A bit like an AI bot might.
Here's some Scratch code to implement this:
And here are instructions for how to do this in Python.
> linear search programming instructions
This program will work like a robot and should guess any number using a linear search.
It is possible to write a program to create a bot to use a binary search as well, although it's quite a lot more complicated!
Here's a program that I wrote to do this job. You can copy and paste the code into a new Python window. Save it and then run the program. Does it work as well as you'd expect?
Program code for binary search – copy and paste the program into a new Python window
How many guesses does it need for a number between 1 and 20? How about 1 to 100? How about 1 to 1000? Is this what you'd expect?
Here's the theory:
How many guesses program – copy and paste the program into a new Python window