PHP | gmp_root() Function

The gmp_root() is an in-built function in PHP which returns the integer part of the N-th root of a GMP number(GNU Multiple Precision: For large numbers).

Syntax:

gmp_pow( $num, $n )

Parameters: The function accepts two mandatory parameters $num and $n.



  1. $num – This is the GMP number whose integer part of the n-th root is returned. The parameter is a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
  2. $n – the positive n-th root of the number. It is an integer value.

Return Value: This function returns a positive GMP number which is the integer part of the N-th root of the $num.

Examples:

Input : $num = "20" $n = 2
Output : 4 

Input : $num = "9" $n = 2
Output : 2

Below programs illustrate the gmp_root() function:

Program 1: The program below demonstrates the working of gmp_root() function when GMP number is passed as argument..

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate the 
// integer part of N-th root of  
// a GMP number 
  
// GMP number as arguments 
$num = gmp_init("1001", 2); 
$n = 3;
  
// function calculates the pow raised to 
// number modulo mod  
      
//  integer part of cubic root of 9
$root = gmp_root($num, $n);  
  
// gmp_strval Convert GMP number to string 
// representation in given base(default 10).
echo gmp_strval($root, 2);
?>

chevron_right


Output:

10

Program 2: The program below demonstrates the working of gmp_root() when numeric string is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate the 
// integer part of N-th root of  
// a GMP number 
  
// GMP number as arguments 
$num = "9"
$n = 3;
  
// function calculates the pow raised to 
// number modulo mod  
      
// integer part of cubic root of 9
$root = gmp_root($num, $n);  
  
echo $root;
?>

chevron_right


Output:

2

Program 3: Program to find the integer part of a square root of a number.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate the 
// integer part of N-th root of  
// a GMP number 
  
// GMP number as arguments 
$num = "25"
$n = 2;
  
// function calculates the pow raised to 
// number modulo mod  
      
// integer part of square root of 25
$root = gmp_root($num, $n);  
  
echo $root;
?>

chevron_right


Output:

5

Reference:
http://php.net/manual/en/function.gmp-root.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.