powered by
Computes the modular inverse of n modulo m.
n
m
modinv(n, m)
integer scalars
a natural number smaller m, if n and m are coprime, else NA.
NA
The modular inverse of n modulo m is the unique natural number 0 < n0 < m such that n * n0 = 1 mod m.
0 < n0 < m
n * n0 = 1 mod m
extGCD
# NOT RUN { modinv(5, 1001) #=> 801, as 5*801 = 4005 = 1 mod 1001 Modinv <- Vectorize(modinv, "n") ((1:10)*Modinv(1:10, 11)) %% 11 #=> 1 1 1 1 1 1 1 1 1 1 # }
Run the code above in your browser using DataLab