Modular inverse: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 6 users not shown)
Line 1,276:
{{trans|AWK}}
<syntaxhighlight lang="easylang">
procfunc mod_inv a b . x1 .
b0 = b
x1 = 1
if b = 1
breakreturn 1
.
while a > 1
Line 1,294:
x1 += b0
.
return x1
.
callprint mod_inv 42 2017 r
print r
</syntaxhighlight>
 
Line 1,793:
{{out}}
<pre>1969</pre>
Alternatively, working from first principles.
<syntaxhighlight lang="java">
public final class ModularInverse {
 
public static void main(String[] aArgs) {
System.out.println(inverseModulus(42, 2017));
}
private static Egcd extendedGCD(int aOne, int aTwo) {
if ( aOne == 0 ) {
return new Egcd(aTwo, 0, 1);
}
Egcd value = extendedGCD(aTwo % aOne, aOne);
return new Egcd(value.aG, value.aY - ( aTwo / aOne ) * value.aX, value.aX);
}
private static int inverseModulus(int aNumber, int aModulus) {
Egcd value = extendedGCD(aNumber, aModulus);
return ( value.aG == 1 ) ? ( value.aX + aModulus ) % aModulus : 0;
}
private static record Egcd(int aG, int aX, int aY) {}
 
}
</syntaxhighlight>
{{ out }}
<pre>
1969
</pre>
 
=={{header|JavaScript}}==
Line 1,862 ⟶ 1,891:
The following code works on any integer type.
To maximize performance, we ensure (via a promotion rule) that the operands are the same type (and use built-ins <code>zero(T)</code> and <code>one(T)</code> for initialization of temporary variables to ensure that they remain of the same type throughout execution).
<syntaxhighlight lang="julia">function modinv{T<:Integer}(a::T, b::T) where T <: Integer
b0 = b
x0, x1 = zero(T), one(T)
Line 2,035 ⟶ 2,064:
<pre>ModularInverse[42, 2017]
1969</pre>
 
=={{header|Maxima}}==
Using built-in function inv_mod
<syntaxhighlight lang="maxima">
inv_mod(42,2017);
</syntaxhighlight>
{{out}}
<pre>
1969
</pre>
 
=={{header|Mercury}}==
Line 2,762 ⟶ 2,801:
 
=={{header|Python}}==
 
===Builtin function===
Since python3.8, builtin function "pow" can be used directly to compute modular inverses by specifying an exponent of -1:
<syntaxhighlight lang="python">>>> pow(42, -1, 2017)
1969
</syntaxhighlight>
 
===Iteration and error-handling===
Line 2,907 ⟶ 2,952:
 
<pre>1969</pre>
 
===Using Extended Euclidean Algorithm===
 
Handles negative args. Returns -1 for non-coprime args.
 
<syntaxhighlight lang="quackery"> [ dup 0 = iff
[ 2drop 1 0 ]
done
dup unrot /mod
dip swap recurse
tuck 2swap *
dip swap - ] is egcd ( n n --> n n )
 
[ dup 0 < if negate
over 0 < if
[ swap negate
over tuck mod
- swap ]
dup rot 2dup egcd
2swap 2over rot *
unrot * + 1 != iff
[ drop 2drop -1 ]
done
nip swap mod ] is modinv ( n n --> n )
 
say " 42 2017 modinv --> " 42 2017 modinv echo cr ( --> 1969 )
say " 40 1 modinv --> " 40 1 modinv echo cr ( --> 0 )
say " 52 -217 modinv --> " 52 -217 modinv echo cr ( --> 96 )
say "-486 217 modinv --> " -486 217 modinv echo cr ( --> 121 )
say " 40 2018 modinv --> " 40 2018 modinv echo cr ( --> -1 )</syntaxhighlight>
 
{{out}}
 
<pre> 42 2017 modinv --> 1969
40 1 modinv --> 0
52 -217 modinv --> 96
-486 217 modinv --> 121
40 2018 modinv --> -1
</pre>
 
=={{header|Racket}}==
Line 3,606 ⟶ 3,690:
=={{header|Wren}}==
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
 
var a = BigInt.new(42)
9,476

edits