Open In App

PHP | gmp_powm() Function

Last Updated : 14 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The gmp_powm() is an inbuilt function in PHP which is used to calculate the number raised to a power of two GMP numbers modulo of another GMP number.(GNU Multiple Precision: For large numbers)
Syntax: 
 

gmp_pow( $base, $exp, $mod)


Parameters: The function accepts three mandatory parameters $base, $exp and $mod 
 

  1. $base - It is the base number.
  2. $exp - It is the power which is raised to the base.
  3. $mod - It returns the remainder after division with $mod


Note: All the parameters are 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. 
Return Value: This function returns a positive GMP number which is equivalent to (baseexp)%mod
Examples: 
 

Input : $base = "2" $exp = "2" $mod = 3
Output : 1

Input : $base = "4" $exp = "2" $mod = 10
Output : 6


Below programs illustrate the gmp_powm() function:
Program 1: The program below demonstrates the working of gmp_powm() function when GMP number are passed as arguments. 
 

php
<?php
// PHP program to calculate power raised 
// to a number modulo mod

// GMP number as arguments 
$base = gmp_init("100", 2);
$exp = gmp_init("10", 2); 
$mod = gmp_init("1010", 2);

// function calculates the pow raised to 
// number modulo mod 
$powm = gmp_powm($base, $exp, $mod);  // 4^2%10 

// gmp_strval converts GMP number to string 
// representation in given base(default 10).
echo gmp_strval($powm, 2);
?>

Output: 
 

110


Program 2: The program below demonstrates the working of gmp_powm() when numeric string are passed as arguments. 
 

php
<?php
// PHP program to calculate power raised 
// to a number modulo m

// numeric strings as arguments
$base = "4";
$exp = "2"; 
$mod = "10";

// function calculates the pow raised to 
// number  4^2%10
$powm = gmp_powm($base, $exp, $mod);

echo $powm;
?>

Output: 
 

6


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


Next Article
Article Tags :
Practice Tags :

Similar Reads