Problem 2635. Higher! Lower!

Created by James in Community

My kids have started to play the "I'm thinking of a number between X and Y" game in the car on trips to and from their swim lessons. I'm trying to teach them the best way to arrive at the correct answer, so I could use a little MATLAB help here to show them what to do...

Using the bisection method (split X and Y, round down if there's a remainder), guess what the number is. Your output should be all of guesses it took for you to get to that number, with the last number in that trail being the number that I'm thinking of.

For example, I'm thinking of a number (8) between 1 and 10.

First guess is (1+10)/2 rounded down, or 5. Five is lower than 8, so your next guess should be (5+10)/2, rounding down to 7. Seven is lower than 8, so your next guess should be (7+10)/2, rounding down to 8.

Since this is the correct answer, your output should be [5 7 8].

The input to the function will be the low bound, the high bound, and the number I'm thinking of. You can assume that they're not cheating, and that the correct answer will be between the high and low bounds. The answer can be equal to the high or low bound, however.

Solution Stats

40.43% Correct | 59.57% Incorrect
Last solution submitted on Jun 04, 2019

Problem Comments