How to resolve the algorithm Nth root step by step in the C programming language
Published on 7 June 2024 03:52 AM
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 ofx
raised toe
. It uses a simple loop to multiplyx
by itselfe
times. -
root
: This is the main function that computes thenth
root ofx
using the Newton-Raphson method. Here's how it works:- It initializes a
double
variabled
and sets the initial guessr
to1
. - It checks special cases:
- If
x
is zero, it returns0
, as thenth
root of zero is always zero. - If
n
is less than 1 or ifx
is negative andn
is even, it returnsNaN
(Not a Number), as those combinations ofn
andx
don't have real roots.
- If
- If none of the special cases apply, it enters a loop:
- It calculates
d
, which is the difference betweenx / r^(n-1)
andr
divided byn
. This difference represents the estimated error in the current guessr
. - It updates the guess
r
by addingd
to it.
- It calculates
- The loop continues until
d
becomes very small (less than10 * DBL_EPSILON
), indicating that the guessr
is accurate enough. - Finally, it returns the computed
nth
rootr
.
- It initializes a
Main Function
- In the
main
function:- It sets
n
to15
andx
to(-3.14159)^15
. This effectively asks for the15th
root of-3.14159
. - It then calls
root(n, x)
to compute thenth
root and prints the result.
- It sets
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