How do you compute pq^−1 mod M?

Revision en1, by daftdove, 2022-12-30 10:35:18

Recently, I saw a problem asking to output the fractional answer p/q as pq^−1 mod M, where p and q are relatively prime in the fraction and M was some prime modulus. My approach was to compute the numerator p and the denominator q separately, and then output p mod M * q^-1 mod M. However, how can I ensure that p and q is relatively prime with this approach? As p and q get gigantic, their factors are lost during the mod, and I can not ensure that p and q are relatively prime.

Thank for reading, and if the question is hard to understand I can clarify.

Tags modular arithmetic, new, trivial

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English daftdove 2022-12-30 10:35:18 591 Initial revision (published)