How does sliding down a tangent line beat halving at finding a square root — and why does it lead straight to functions?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Finding the square root of k means finding a root of f(x) = x**2 - k. To take a Newton step we first need f evaluated at the current guess. For k = 24 and a guess of 12, compute f(guess) and assign it to f_value.
(Hint: ** raises a number to a power.)
The update g - f(g)/f'(g)
State the general update, then put f = x**2 - k and f' = 2*x into it to get g - (g**2 - k)/(2*g), and draw the tangent line running from a guess to the x-axis.
The Newton-Raphson code and its guess counts
Write the loop with the starting guess k/2 and the update line, keeping the epsilon test unchanged, and record 4 guesses for k = 24 and 10 guesses for 54321.
Three shared parts of guess-and-check
Name the loop, the guess-generation step and the epsilon check, then tabulate exhaustive enumeration, approximation, bisection search and Newton-Raphson by the guess-generation each one uses.
Decomposition and abstraction
Define decomposition as self-contained parts that communicate and abstraction as hiding implementation behind an interface, with the saved variables pi and r and a code comment as the small examples.