PHP | gmp_invert() for inverse modulo
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 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 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
Recommended Posts:
- How to get negative result using modulo operator in JavaScript ?
- How to hide or show one record from an ng-repeat within a table based on ng-click?
- How to check whether an array is subset of another array using JavaScript ?
- How to set placeholder value for input type date in HTML 5 ?
- How to check if a number evaluates to Infinity using JavaScript ?
- How to print a number with commas as thousands separators in JavaScript?
- HTML | Marquee scrolldelay attribute
- HTML | Marquee stop Method
- HTTP headers | Cookie
- HTML | Marquee truespeed attribute
- HTML | Marquee start method
- HTML | Marquee scrollamount attribute
- HTTP headers | X-Forwarded-Proto
- HTML | Marquee height attribute
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.



