Problem 2635. Higher! Lower!
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
Problem Comments
-
8 Comments
Show
5 older comments
Michael Jarboe
on 4 Jun 2019
Emphasized Alfonso Nieto-Castanon Comments "your example ([5 7 8]) is not really following the optimal bisection approach. After you choose 5, the new bounds are not 5 and 10, they are 6 and 10, so your next choice should be 8 ((6+10)/2) instead of 7..."
Nikolaos Nikolaou
on 9 Sep 2019
test suit is broken plz fix since its a budge problem
Alex
on 23 Sep 2020
I used this idea to create a similar problem that requires solvers to apply the bisection method correctly (see previous comments). https://www.mathworks.com/matlabcentral/cody/problems/46603-higher-lower-correct
Solution Comments
Show commentsProblem Recent Solvers38
Suggested Problems
-
406 Solvers
-
366 Solvers
-
Output any real number that is neither positive nor negative
399 Solvers
-
581 Solvers
-
Mersenne Primes vs. All Primes
723 Solvers
More from this Author80
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!