2019-09-12 10:33:56 -07:00
|
|
|
proc modInv(a0, b0: int): int =
|
2016-12-05 23:44:36 +01:00
|
|
|
var (a, b, x0) = (a0, b0, 0)
|
|
|
|
|
result = 1
|
|
|
|
|
if b == 1: return
|
|
|
|
|
while a > 1:
|
2019-09-12 10:33:56 -07:00
|
|
|
result = result - (a div b) * x0
|
2016-12-05 23:44:36 +01:00
|
|
|
a = a mod b
|
|
|
|
|
swap a, b
|
|
|
|
|
swap x0, result
|
|
|
|
|
if result < 0: result += b0
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
echo modInv(42, 2017)
|