RosettaCodeData/Task/Modular-inverse/FunL/modular-inverse.funl

13 lines
226 B
Plaintext

import integers.egcd
def modinv( a, m ) =
val (g, x, _) = egcd( a, m )
if g != 1 then error( a + ' and ' + m + ' not coprime' )
val res = x % m
if res < 0 then res + m else res
println( modinv(42, 2017) )