0

For the equation $(am) \mod n = r$ for integers $a, n, r$, how can we find $m$ efficiently, if such an $m$ exists?

One very basic way I thought of was to simply enumerate all the valid $m$ from $0, \ldots, n-1$ and check them. But this can be quite slow if $n$ is large. Is there a number-theoretic way to speed this up?

  • 2
    Yes: do you know how to find the modular inverse $a^{-1}\pmod n$ using the extended Euclidean algorithm? You can then calculate $m\equiv a^{-1}r\pmod n$. (If $a$ and $n$ have common factors, divide through by those first.) – Greg Martin Sep 21 '22 at 04:10
  • Solve $,ax\equiv r\pmod{!n},$ by the methods in the linked dupe. – Bill Dubuque Sep 21 '22 at 10:28

0 Answers0