6

1

I'm using Mathematica to illustrate basic number theory concepts in a graduate cryptography class. To generate elements of the multiplicative group of integers modulo $n$, i.e. $\mathbb{Z}^*_n$, I can use the following code:

```
Z[n_] := Table[If[GCD[i, n] == 1, i, ## &[]], {i, n}];
```

Example:

```
In: Z[21]
Out: {1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20}
```

The notation `## &[]`

used in defining `Z[n_]`

will certainly raise questions for students who have no prior exposure to Mathematica. Is there a simpler way of generating elements of $\mathbb{Z}^*_n$?

**PS:** I can use:

```
Z[n_] := DeleteCases[Table[If[GCD[i, n] == 1, i], {i, n}], Null];
```

but that's not elegant either.

How about a

`Select`

over a simple range? – wxffles – 2015-03-11T23:39:48.547`Z[n_] := Select[CoprimeQ[#, n] &]@Range[1, n]`

– evanb – 2015-03-11T23:45:57.127@evanb I just posted something very similar - simultaneously with your comment... – Jens – 2015-03-11T23:47:06.617

`Position[MultiplicativeOrder[21, #] & /@ Range@20, _?IntegerQ, 1]`

– Dr. belisarius – 2015-03-12T00:08:54.017`Select[Range[21], NumericQ@MultiplicativeOrder[21, #] &]`

– Dr. belisarius – 2015-03-12T00:13:18.533