Skip to content
QuantReadySign In
#446easyNumerical Methods

Newton-Raphson Cube Root

Time Limit: 2sMemory: 256MB

Problem

Implement Newton-Raphson root finding to compute the cube root of a positive number aa.

Define f(x)=x3af(x) = x^3 - a. The root of ff is x=a1/3x = a^{1/3}. Newton's method iterates:

xn+1=xnf(xn)f(xn)=xnxn3a3xn2x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} = x_n - \frac{x_n^3 - a}{3x_n^2}

Starting from initial guess x0x_0, run exactly num_iterations iterations and output the final xx.

Input Format

Three space-separated values: a x0 num_iterations

Output Format

The final approximation of a1/3a^{1/3}, to 4 decimal places.

Examples

Example 1
Input(Three space-separated values: a x0 numiterations)
8.0 1.0 10
Output
2.0000

Cube root of 8 is 2.0. Starting from x0=1.0, Newton converges quickly.

Example 2
Input(Three space-separated values: a x0 numiterations)
2.0 1.0 6
Output
1.2599

Cube root of 2 is approximately 1.2599.

Constraints

  • 0.001 ≤ a ≤ 10^6
  • x0 > 0
  • 1 ≤ num_iterations ≤ 100
  • Output to 4 decimal places
Loading interactive editor…