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
modsqrt( 8, 23) # 10 because 10^2 = 100 = 8 mod 23
modsqrt(10, 17) # 0 because 10 is not a quadratic residue mod 17
Run the code above in your browser using DataLab