TITLE: Fast computation of multiplicative inverse modulo q
QUESTION [15 upvotes]: Given a large number $q$ (say, a prime) and a number $a$ between 2 and $q-1$ what is the fastest algorithm known for computing the inverse of $a$ in the group of residue classes modulo $q$?

REPLY [6 votes]: Another good reference is
http://www.loria.fr/~zimmerma/mca/pub226.html
Other than the Euclidean algorithm as described in other answers, you can get time $O(M(n))$ if the modulus is of the form $p^k$, by working successively mod $p$, $p^2$, etc.  See the link for details, section 2.5.
If you need to invert several numbers, say $x$ and $y$, it is often faster to invert $xy$ and then calculate $x^{-1} = (xy)^{-1}y$, $y^{-1} = (xy)^{-1}x$.