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
Solution Comments
Show comments
Loading...
Problem Recent Solvers40
Suggested Problems
-
46500 Solvers
-
268 Solvers
-
200 Solvers
-
2227 Solvers
-
841 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!
Test suite should have assert(isequal(...)).
It's an interesting problem. Unfortunately without any assert, the test suite does not test anything. So any syntactically correct answer succeeds even if it doesn't solve the problem.
Fixed, and (hopefully) rescored. Thanks for the heads up.
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...
The misplaced comment line in the first test case breaks the test suite (at least for "conventional" solutions).
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..."
test suit is broken plz fix since its a budge problem
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