How to resolve the algorithm Nth root step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Nth root step by step in the C programming language

Table of Contents

Problem Statement

Implement the algorithm to compute the principal   nth   root

A

n

{\displaystyle {\sqrt[{n}]{A}}}

of a positive real number   A,   as explained at the   Wikipedia page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Nth root step by step in the C programming language

The given C code calculates the nth root of a given positive number x using the Newton-Raphson method. Specifically, it computes the 15th root of -3.14159.

Function Breakdown

  • pow_: This function calculates the power of x raised to e. It uses a simple loop to multiply x by itself e times.

  • root: This is the main function that computes the nth root of x using the Newton-Raphson method. Here's how it works:

    • It initializes a double variable d and sets the initial guess r to 1.
    • It checks special cases:
      • If x is zero, it returns 0, as the nth root of zero is always zero.
      • If n is less than 1 or if x is negative and n is even, it returns NaN (Not a Number), as those combinations of n and x don't have real roots.
    • If none of the special cases apply, it enters a loop:
      • It calculates d, which is the difference between x / r^(n-1) and r divided by n. This difference represents the estimated error in the current guess r.
      • It updates the guess r by adding d to it.
    • The loop continues until d becomes very small (less than 10 * DBL_EPSILON), indicating that the guess r is accurate enough.
    • Finally, it returns the computed nth root r.

Main Function

  • In the main function:
    • It sets n to 15 and x to (-3.14159)^15. This effectively asks for the 15th root of -3.14159.
    • It then calls root(n, x) to compute the nth root and prints the result.

Output

  • The code should print something like this:
    root(15, -32623.63691828039) = -3.141592653589793
    

Source code in the c programming language

#include <stdio.h>
#include <float.h>

double pow_ (double x, int e) {
    int i;
    double r = 1;
    for (i = 0; i < e; i++) {
        r *= x;
    }
    return r;
}

double root (int n, double x) {
    double d, r = 1;
    if (!x) {
        return 0;
    }
    if (n < 1 || (x < 0 && !(n&1))) {
        return 0.0 / 0.0; /* NaN */
    }
    do {
        d = (x / pow_(r, n - 1) - r) / n;
        r += d;
    }
    while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
    return r;
}

int main () {
    int n = 15;
    double x = pow_(-3.14159, 15);
    printf("root(%d, %g) = %g\n", n, x, root(n, x));
    return 0;
}


  

You may also check:How to resolve the algorithm Atomic updates step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Lasso programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Plain English programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the PL/I programming language