MHB Computing The Arbitrary-Precision Nth Root Of Positive (X) In PHP

AI Thread Summary
PHP's arbitrary-precision arithmetic functions, particularly the BC (binary calculator) functions, allow for advanced computations, including calculating Nth roots beyond basic operations. To compute the Nth root of a positive number, a custom function is required, utilizing an iterative approximation method based on Newton's formula. The function iteratively refines an initial approximation until the desired precision is achieved, allowing for results rounded to any specified number of decimal places. An example implementation demonstrates how to compute the 5th root of 100 with high precision. This approach can be integrated into a library of custom PHP math functions for developers.
Jay1
Messages
12
Reaction score
0
PHP has some powerful arbitrary-precision (BC or binary calculator) arithmetic functions which seem to be greatly underused and only consist of the basic arithmetic operations, a square root function, a mod function and an integer power function.

However, those basic operations can be used to perform some very fancy computations much harder to do in most other languages used on the WWW.

There is an arbitrary-precision square root function, but what if we wanted the arbitrary-precision cube root or sixth root, etc.?

For roots higher than the square root, we have to create a custom function built from the basic operations.

Given:
a = Current approximation to Nth root of (x)
N = Root for which to solve (2,3,4 ...)
x = Argument for which to find the Nth root
b = Next generation approximation derived from current generation (a)

Given any initial starting approximation (a) to the Nth root of (x),
the next approximation (b) may be computed by the general formula:

$$b = \frac{\frac{x}{a^{N-1}} + a\cdot(N-1)}{N}$$

The resulting value of (b) is then substituted as the new value of (a) and the process is repeated, each time giving a closer and closer approximation (b) of the Nth root of (x) until it reaches whatever level of precision (decimals) we require.

Below is an arbitrary-precision PHP function designed to compute the Nth root of positive argument (x) to any given number of decimals based on the above recursion formula.

The argument (x) should be given in the form of a numerical string, such as "12.345"

Code:
   function bcNth_Root_Of_x ($N, $x, $NumDecimals=16)
{
   $a = sprintf("%1.16f", pow($x, 1/$N));
   $n = $N - 1;
   $d = $NumDecimals;
   $D = $d + 8;

   for ($i=1;  $i <= 100;   $i++)
  {
   $b = bcdiv(bcadd(bcmul($n,$a,$D), bcdiv($x, bcpow($a,$n,$D),$D),$D),$N,$D);

   if (bccomp($a,$b,$D) == 0) {break;} else {$a = $b;}
  }
   return rtrim(rtrim(bcadd($b, "0." . str_repeat(0,$d) . "5", $d), "0"), ".");
}

EXAMPLE:
Below is a small, ready-to-run PHP example program calling the function to compute the 5th root of 100, rounded to 75 decimals.

Code:
<?php

$N = 5;
$x = "100";
$decimals = 75;

print bcNth_Root_Of_x ($N, $x, $decimals);

// ------------------------------------------------

 function bcNth_Root_Of_x ($N, $x, $NumDecimals=16)
{
 $a = sprintf("%1.16f", pow($x, 1/$N));
 $n = $N - 1;
 $d = $NumDecimals;
 $D = $d + 8;

  for ($i=1;  $i <= 100;   $i++)
 {
  $b = bcdiv(bcadd(bcmul($n,$a,$D), bcdiv($x, bcpow($a,$n,$D),$D),$D),$N,$D);

  if (bccomp($a,$b,$D) == 0) {break;} else {$a = $b;}
 }
  return rtrim(rtrim(bcadd($b, "0." . str_repeat(0,$d) . "5", $d), "0"), ".");
}

?>
The returned result should be:
Code:
2.511886431509580111085032067799327394158518100782475428679888420908243247724

Here is a commented listing of the above function.

Code:
/*
   ------------------------------------------------------------------
   This function computes the arbitrary-precision Nth root of a given
   numerical string (x), rounded at any specified number of decimals.

   ARGUMENTS:
   N = Root = Integer 2, 3, 4, ..., etc.
   x = Numerical argument string for which we seek the Nth root.

   NumDecimals = Number of decimals at which to round off the result
                 if exact resolution is not possible.
   ERRORS:
   NO special error checking is done.
   ------------------------------------------------------------------
*/

   function bcNth_Root_Of_x ($N, $x, $NumDecimals=16)
{
// Compute first seed approximation.
   $a = sprintf("%1.16f", pow($x, 1/$N));
   $n = $N - 1;
   $d = $NumDecimals;

// Add 8 extra decimals precision as rounding fuzz buffer
   $D = $d + 8;

// Initialize iteration control loop.  The limit is set to 100 to prevent
// an infinite loop lockup, which would hang the function.  This limit
// should be far more than sufficient and never likely to be reached.
   for ($i=1;  $i <= 100;   $i++)
  {
// Compute next generation approximation (b) to Nth root of (x) from the
// current generation approximation (a).
   $b = bcdiv(bcadd(bcmul($n,$a,$D), bcdiv($x, bcpow($a,$n,$D),$D),$D),$N,$D);

// If (a == b) to desired precision, then done, (b) is root.
// Else current (b) becomes the new (a) for the next cycle.
   if (bccomp($a,$b,$D) == 0) {break;} else {$a = $b;}
  }
// Round off root to (d) decimals, then trim redundant zeros and/or decimal.
   return rtrim(rtrim(bcadd($b, "0." . str_repeat(0,$d) . "5", $d), "0"), ".");
}

If you happen to be a PHP math coder, like me, this Nth root function could be part of a library of custom arbitrary-precision PHP math functions.

Some example test values are:
Code:
Cube (3) root of 10 to 32 decimals:
= 2.15443469003188372175929356651935

7th root of 18435.57209 to 50 decimals:
= 4.0679866312376590401866108430333983477514

4th root of 4646545.65686847759871677987 to 80 decimals:
= 46.42827543473103234672521339464313742938263307554617373866851061590663732382853491

etc.
Cheers and tally-ho

P.S.
The formula given above is based on Newton's method for finding roots.

Ref:
https://en.wikipedia.org/wiki/Newton's_method:)
 
Last edited:
Mathematics news on Phys.org
Jay said:
PHP has some powerful arbitrary-precision (BC or binary calculator) arithmetic functions which seem to be greatly underused and only consist of the basic arithmetic operations, a square root function, a mod function and an integer power function.

However, those basic operations can be used to perform some very fancy computations much harder to do in most other languages used on the WWW.

There is an arbitrary-precision square root function, but what if we wanted the arbitrary-precision cube root or sixth root, etc.?

For roots higher than the square root, we have to create a custom function built from the basic operations.

Given:
a = Current approximation to Nth root of (x)
N = Root for which to solve (2,3,4 ...)
x = Argument for which to find the Nth root
b = Next generation approximation derived from current generation (a)

Given any initial starting approximation (a) to the Nth root of (x),
the next approximation (b) may be computed by the general formula:

$$b = \frac{\frac{x}{a^{N-1}} + a\cdot(N-1)}{N}$$

The resulting value of (b) is then substituted as the new value of (a) and the process is repeated, each time giving a closer and closer approximation (b) of the Nth root of (x) until it reaches whatever level of precision (decimals) we require.

Below is an arbitrary-precision PHP function designed to compute the Nth root of positive argument (x) to any given number of decimals based on the above recursion formula.

The argument (x) should be given in the form of a numerical string, such as "12.345"

Code:
   function bcNth_Root_Of_x ($N, $x, $NumDecimals=16)
{
   $a = sprintf("%1.16f", pow($x, 1/$N));
   $n = $N - 1;
   $d = $NumDecimals;
   $D = $d + 8;

   for ($i=1;  $i <= 100;   $i++)
  {
   $b = bcdiv(bcadd(bcmul($n,$a,$D), bcdiv($x, bcpow($a,$n,$D),$D),$D),$N,$D);

   if (bccomp($a,$b,$D) == 0) {break;} else {$a = $b;}
  }
   return rtrim(rtrim(bcadd($b, "0." . str_repeat(0,$d) . "5", $d), "0"), ".");
}

EXAMPLE:
Below is a small, ready-to-run PHP example program calling the function to compute the 5th root of 100, rounded to 75 decimals.

Code:
<?php

$N = 5;
$x = "100";
$decimals = 75;

print bcNth_Root_Of_x ($N, $x, $decimals);

// ------------------------------------------------

 function bcNth_Root_Of_x ($N, $x, $NumDecimals=16)
{
 $a = sprintf("%1.16f", pow($x, 1/$N));
 $n = $N - 1;
 $d = $NumDecimals;
 $D = $d + 8;

  for ($i=1;  $i <= 100;   $i++)
 {
  $b = bcdiv(bcadd(bcmul($n,$a,$D), bcdiv($x, bcpow($a,$n,$D),$D),$D),$N,$D);

  if (bccomp($a,$b,$D) == 0) {break;} else {$a = $b;}
 }
  return rtrim(rtrim(bcadd($b, "0." . str_repeat(0,$d) . "5", $d), "0"), ".");
}

?>
The returned result should be:
Code:
2.511886431509580111085032067799327394158518100782475428679888420908243247724

Here is a commented listing of the above function.

Code:
/*
   ------------------------------------------------------------------
   This function computes the arbitrary-precision Nth root of a given
   numerical string (x), rounded at any specified number of decimals.

   ARGUMENTS:
   N = Root = Integer 2, 3, 4, ..., etc.
   x = Numerical argument string for which we seek the Nth root.

   NumDecimals = Number of decimals at which to round off the result
                 if exact resolution is not possible.
   ERRORS:
   NO special error checking is done.
   ------------------------------------------------------------------
*/

   function bcNth_Root_Of_x ($N, $x, $NumDecimals=16)
{
// Compute first seed approximation.
   $a = sprintf("%1.16f", pow($x, 1/$N));
   $n = $N - 1;
   $d = $NumDecimals;

// Add 8 extra decimals precision as rounding fuzz buffer
   $D = $d + 8;

// Initialize iteration control loop.  The limit is set to 100 to prevent
// an infinite loop lockup, which would hang the function.  This limit
// should be far more than sufficient and never likely to be reached.
   for ($i=1;  $i <= 100;   $i++)
  {
// Compute next generation approximation (b) to Nth root of (x) from the
// current generation approximation (a).
   $b = bcdiv(bcadd(bcmul($n,$a,$D), bcdiv($x, bcpow($a,$n,$D),$D),$D),$N,$D);

// If (a == b) to desired precision, then done, (b) is root.
// Else current (b) becomes the new (a) for the next cycle.
   if (bccomp($a,$b,$D) == 0) {break;} else {$a = $b;}
  }
// Round off root to (d) decimals, then trim redundant zeros and/or decimal.
   return rtrim(rtrim(bcadd($b, "0." . str_repeat(0,$d) . "5", $d), "0"), ".");
}

If you happen to be a PHP math coder, like me, this Nth root function could be part of a library of custom arbitrary-precision PHP math functions.

Some example test values are:
Code:
Cube (3) root of 10 to 32 decimals:
= 2.15443469003188372175929356651935

7th root of 18435.57209 to 50 decimals:
= 4.0679866312376590401866108430333983477514

4th root of 4646545.65686847759871677987 to 80 decimals:
= 46.42827543473103234672521339464313742938263307554617373866851061590663732382853491

etc.
Cheers and tally-ho

P.S.
The formula given above is based on Newton's method for finding roots.

Ref:
https://en.wikipedia.org/wiki/Newton's_method:)
please help me code C# with algorthrm this
 
Seemingly by some mathematical coincidence, a hexagon of sides 2,2,7,7, 11, and 11 can be inscribed in a circle of radius 7. The other day I saw a math problem on line, which they said came from a Polish Olympiad, where you compute the length x of the 3rd side which is the same as the radius, so that the sides of length 2,x, and 11 are inscribed on the arc of a semi-circle. The law of cosines applied twice gives the answer for x of exactly 7, but the arithmetic is so complex that the...
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...
Back
Top