TITLE: How to compute $\mathbb{Z}_n^*$, the unit group of the integers modulo $n$?
QUESTION [5 upvotes]: I am trying to wrap my head around calculating residue classes, for example $\mathbb{Z}^*_{12} = [1]...$
Would anyone mind making up an example with a decent explanation. I just wrap my head around things easier seeing the process.
Thanks

REPLY [5 votes]: The units of $\rm\:\Bbb Z_{12}\:$ are the residue classes coprime to $\,12.\:$ We can compute them by sieving in a way similar to Eratosthenes' prime sieve. Make a list of the residue classes, then successively exclude (cross/grey out) elements $> 1\,$ that divide the modulus, along with  all of their multiples. Starting with $\,2,\,$ since $\rm\:2\mid 12\:$ we grey out $\,2\,$ and all its multiples, i.e. all even classes.
$$\begin{array}{}1 & \color{#ccc}2 & 3 & \color{#ccc}4 & 5 & \color{#ccc}6 & 7 & \color{#ccc}8 & 9 & \color{#ccc}{10} & 11\end{array}$$
The next unprocessed number is $\,3,\,$ and $\rm\:3\mid 12,\:$ so we grey out it and its multiples.
$$\begin{array}{}1 & \color{#ccc}2 & \color{#ccc}3 & \color{#ccc}4 & 5 & \color{#ccc}6 & 7 & \color{#ccc}8 & \color{#ccc}9 & \color{#ccc}{10} & 11\end{array}$$
The next unprocessed number is $\,5,\,$ and $\:5\nmid 12,\:$ so we don't grey it out, but move on to the next unprocessed number $\,7,\,$ etc.  What remains is $\ 1,\,5,\,7,\,11,\:$ which are all of the units of $\:\Bbb Z_{12}.$
As for why the units (invertibles) are those elements coprime to the modulus $\rm\:m,\:$ this is an immediate consequence of the Bezout identity for the gcd, namely
$$\rm\begin{eqnarray} gcd(a,m) = 1\ \iff\,\ \ 1\, &=&\rm\, j\,a + k\,m \ \ \ for\ \ some\ \ j,k\in\Bbb Z\\
\rm \iff\, \ \ 1\,&\equiv&\rm\, j\,a\ \ (mod\ m)\\
\iff\,\ [1]\! &=&\rm\! [j][a]\ \ in\ \ \Bbb Z_m
\end{eqnarray}$$