How can a square-root search creep upward until it's 'close enough' — yet loop forever on 54321 until one extra stop condition saves it?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The "good enough" test measures how far the square of guess lands from x. For the
values below — x = 36 and a guess of 5.5 — compute that distance and assign it to
distance. A distance is never negative.
(Hint: **2 squares a number and abs() gives the absolute value.)
The approximation loop for a square root
Write the while abs(guess**2 - x) >= epsilon loop that starts the guess at 0 and adds the increment each pass, and trace it on 36 to 5.9992 after about 59,992 guesses.
Epsilon and increment as the two parameters
State that a smaller increment buys accuracy at the price of more guesses while a larger epsilon finishes sooner with a looser answer, and record the speed-versus-accuracy trade-off.
The overshoot on 54321
Record the debug printouts where the gap narrows to about 1,400 at a guess near 230, widens to about 3,000 at 240, reaches nearly 200,000 by 500, and the loop runs on forever.
The extra guess**2 > x stopping condition
Write the second exit test and the if/else reporting success or failure, then record that an increment of 0.00001 returns 233.06864 after about 23 million guesses.