PHP | gmp_invert() for inverse modulo Last Updated : 14 Apr, 2018 Comments Improve Suggest changes Like Article Like Report The gmp_invert() is a built-in function in PHP which is used to find the modular inverse of a GMP number (GNU Multiple Precision : For large numbers) under another GMP number. The modular inverse is a number x such that: a x ≡ 1 (mod b) The value of x should be in {0, 1, 2, … b-1}, i.e., in the ring of integer modulo b. Syntax: gmp_invert ( $a, $b ) Parameters: This function accepts two GMP numbers $a and $b as shown in the above syntax. This function finds the inverse of $a under modulo $b. These parameters can be 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 GMP number which is the calculated inverse modulo of the two numbers passed to it as arguments. If it is not possible to find the inverse modulo for the given two numbers then this function returns FALSE. Examples: Input: $a = 3, $b = 11 Output: 4 Since (4*3) mod 11 = 1, 4 is modulo inverse of 3 One might think, 15 also as a valid output as "(15*3) mod 11" is also 1, but 15 is not in ring {0, 1, 2, ... 10}, so not valid. Input: $a = 10, $b = 17 Output: 12 Since (10*12) mod 17 = 1, 12 is modulo inverse of 3 Below programs illustrate the gmp_invert() function in PHP : Program 1: Program to calculate the inverse modulo when numeric strings as GMP numbers are passed as arguments. php <?php // PHP program to calculate inverse modulo // strings as GMP numbers $a = "3"; $b = "11"; // calculates the inverse modulo of a number $invMod = gmp_invert($a, $b); echo $invMod."\n"; // calculates the inverse modulo of a number $a = "10"; $b = "17"; $invMod = gmp_invert($a, $b); echo $invMod."\n"; ?> Output: 4 12 Program 2: Program to calculate the inverse modulo when GMP numbers are passed as arguments. php <?php // PHP program to calculate inverse modulo // creating GMP numbers using gmp_init() $a = gmp_init(3, 10); $b = gmp_init(11, 10); // calculates the inverse modulo of a number $invMod = gmp_invert($a, $b); echo $invMod."\n"; // calculates the inverse modulo of a number $a = gmp_init(10, 10); $b = gmp_init(17, 10); $invMod = gmp_invert($a, $b); echo $invMod."\n"; ?> Output: 4 12 Reference: http://php.net/manual/en/function.gmp-invert.php Comment More infoAdvertise with us Next Article PHP | gmp_invert() for inverse modulo B barykrg Follow Improve Article Tags : Misc Web Technologies PHP PHP-gmp Practice Tags : Misc Similar Reads PHP gmp_init() Function The gmp_init() function is an inbuilt function in PHP that is used to create a GMP number from different data types, including strings, integers, or other GMP objects. It's commonly used when you want to start performing arithmetic operations on large numbers without losing precision. Syntax: gmp_in 2 min read PHP | gmp_intval() Function The gmp_intval() is an inbuilt function in PHP which converts a GMP number to an integer. Here GMP refers to GNU Multiple Precision which is for large numbers.Syntax:Â Â int gmp_intval ( $num ) Parameters: The function accepts a single parameter $num which is a GMP number and returns its integer valu 2 min read PHP | gmp_nextprime() Function The gmp_nextprime() is an inbuilt function in PHP which is used to calculate the prime number just greater than a given GMP number(GNU Multiple Precision : For large numbers). Syntax: gmp_nextprime($num) Parameters: This function accepts a GMP number $num as a mandatory parameter as shown in the abo 2 min read PHP | gmp_mul() Function The gmp_mul() function in PHP is an inbuilt function which is used to multiply two GMP numbers (GNU Multiple Precision: For large numbers). Syntax: GMP gmp_mul ( GMP $num1, GMP $num2 ) Parameters: This function accepts two GMP numbers. It is mandatory parameters as shown in the above syntax. These c 1 min read PHP gmp_legendre() Function The gmp_legendre() function is an in-built function in PHP which computes the Legendre symbol of two GMP numbers (GNU Multiple Precision: For large numbers) $num1 and $num2 passed as parameters to the function and returns it. $num2 must be positive and odd. Syntax: gmp_legendre( $num1, $num2 ) Param 2 min read PHP | gmp_add() for adding large numbers The gmp_add() is a built-in function in PHP which is used to add two GMP numbers (GNU Multiple Precision : For large numbers). Syntax : gmp_add ( $num1, $num2 ) Parameters: This function accepts two GMP numbers as mandatory parameters as shown in the above syntax. These can be a GMP object in PHP ve 1 min read Modular multiplicative inverse from 1 to n Give a positive integer n, find modular multiplicative inverse of all integer from 1 to n with respect to a big prime number, say, 'prime'. The modular multiplicative inverse of a is an integer 'x' such that. a x ? 1 (mod prime) Examples: Input : n = 10, prime = 17 Output : 1 9 6 13 7 3 5 15 2 12 Ex 9 min read PHP | IntlChar::forDigit() Function The IntlChar::forDigit() function is an inbuilt function in PHP which is used to determines the character representation for a specific digit in the specified radix. Syntax: int IntlChar::forDigit( $digit, $radix ) Parameters: This function accepts two parameters as mentioned above and described bel 2 min read PHP | gmp_import() Function The gmp_import() function is an inbuilt function in php which imports a GMP number(GNU Multiple Precision: For large numbers) from a binary string. Syntax: GMP gmp_import ( string $data, int $word_size, int $options ) Parameters: The gmp_import() function accepts three parameters as mentioned above 2 min read PHP | gmp_neg() Function The gmp_neg() function is an in-built function in PHP which returns the negative of a GMP number (GNU Multiple Precision). Syntax : gmp_neg( $num ) Parameters : The function accepts only one mandatory parameter $num which can be either a GMP number resource in PHP 5.5 or a GMP object in PHP version 1 min read Like