How to resolve the algorithm Modular inverse step by step in the PHP programming language
How to resolve the algorithm Modular inverse step by step in the PHP programming language
Table of Contents
Problem Statement
From Wikipedia: In modular arithmetic, the modular multiplicative inverse of an integer a modulo m is an integer x such that Or in other words, such that: It can be shown that such an inverse exists if and only if a and m are coprime, but we will ignore this for this task.
Either by implementing the algorithm, by using a dedicated library or by using a built-in function in your language, compute the modular inverse of 42 modulo 2017.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Modular inverse step by step in the PHP programming language
The provided PHP code defines a function invmod
that calculates the modular inverse of a given number a
modulo n
. The modular inverse is the integer x
such that (a * x) % n == 1
. It uses the extended Euclidean algorithm to efficiently compute the modular inverse.
Here's a step-by-step explanation of the code:
-
The function
invmod
takes two parameters:$a
: The integer for which we want to find the modular inverse.$n
: The modulus.
-
It first checks if
$n
is negative. If it is,$n
is negated (multiplied by -1). -
It then checks if
$a
is negative. If it is,$a
is replaced with the remainder of-$a
divided by$n
. This ensures that$a
is always positive. -
It initializes six variables:
$t
,$nt
: These variables represent the coefficients of the formt * a + nt * n
that will be used to find the modular inverse.$r
,$nr
: These variables represent the remainders and coefficients in the Euclidean algorithm.
-
The code enters a
while
loop that continues as long as$nr
is not equal to 0. -
Inside the loop, it calculates the quotient
$quot
as the integer division of$r
by$nr
. -
It updates
$t
,$nt
,$r
, and$nr
using the following formulas:$tmp = $nt; $nt = $t - $quot * $nt; $t = $tmp;
$tmp = $nr; $nr = $r - $quot * $nr; $r = $tmp;
These formulas are derived from the extended Euclidean algorithm.
-
The loop continues until
$nr
becomes 0. If$nr
is not 0 after the loop, it means that$a
and$n
do not have a modular inverse, and the function returns -1. -
If
$r
is greater than 1 after the loop, it means that$a
and$n
are not coprime, and the function returns -1. -
If
$t
is negative after the loop, it is adjusted by adding$n
to it. -
Finally, the function returns
$t
, which is the modular inverse of$a
modulo$n
.
In the example usage provided in the code, it calculates invmod(42, 2017)
and prints the result, which is 1008.
Source code in the php programming language
<?php
function invmod($a,$n){
if ($n < 0) $n = -$n;
if ($a < 0) $a = $n - (-$a % $n);
$t = 0; $nt = 1; $r = $n; $nr = $a % $n;
while ($nr != 0) {
$quot= intval($r/$nr);
$tmp = $nt; $nt = $t - $quot*$nt; $t = $tmp;
$tmp = $nr; $nr = $r - $quot*$nr; $r = $tmp;
}
if ($r > 1) return -1;
if ($t < 0) $t += $n;
return $t;
}
printf("%d\n", invmod(42, 2017));
?>
You may also check:How to resolve the algorithm Logical operations step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Classes step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Jacobsthal numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Factorial step by step in the Piet programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Ol programming language