Previous

Functions

6 / 6
Next

The Newton-Raphson method can be used to find approximations of the $n$-th root $x=\sqrt[n]{S}$ of a number $S$. For an approximation, we try to solve $$x^n−S=0$$ If $\widehat x$ is a guess to the value of $\sqrt[n]{S}$, then the following formula is always a better guess: $$ \widehat x - \frac {{\widehat x}^n - S}{n⋅\widehat{x}^{n-1}} $$ We can implement this recursively until the difference between consecutive guesses is smaller than some tolerance.

Send

Capstone

Hint
Advanced

Implement a Python function sqrt(S, n) that approximates the $n$-th root of a positive number $S$ using the Newton-Raphson method.

  • Use a tolerance of 1e-8 (that is, 0.00000001).
  • The value of n should default to $2$.

Hint: the $n$-th power of a variable x can be computed in Python with x**n.

Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!