You can trap between two numbers and halve the gap again and again. After halvings the trap is far narrower than a millionth.
The idea. Start with . Try the middle. If its square is below , the root is in the upper half; if not, it is in the lower half.
Your tasks.
middle, return the number halfway between lo and hi.step, return the new pair (lo, hi) that still holds the root of n.root to the middle of the trap after steps for , starting from (2, 3).Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…