---
title: "The Unit Group"
canonical: "https://docs.magma-maths.org/v2.30-1/BasicRings/ResidueClassRings/unit-group.html"
source: "https://docs.magma-maths.org/v2.30-1/BasicRings/ResidueClassRings/unit-group.md"
part: "Basic Rings"
chapter: "Integer Residue Class Rings"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Basic Rings", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/index-basic-rings.md"}
  - {"title": "Integer Residue Class Rings", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/ResidueClassRings/index-residue-class-rings.md"}
prev: {"title": "Ideal Operations", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/ResidueClassRings/ideal-ops.md"}
next: {"title": "Dirichlet Characters", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/ResidueClassRings/dirichlet-characters.md"}
intrinsics:
  - {"name": "UnitGroup", "signature": "UnitGroup(R): RngIntRes -> GrpAb, Map"}
  - {"name": "IsPrimitive", "signature": "IsPrimitive(n): RngIntResElt -> BoolElt"}
  - {"name": "PrimitiveElement", "signature": "PrimitiveElement(R): RngIntRes -> RngIntResElt"}
  - {"name": "PrimitiveRoot", "signature": "PrimitiveRoot(R): RngIntRes -> RngIntResElt"}
  - {"name": "Order", "signature": "Order(a): RngIntResElt -> RngIntElt"}
  - {"name": "Normalize", "signature": "Normalize(x): RngIntRes -> RngIntResElt, RngIntResElt"}
  - {"name": "Normalise", "signature": "Normalise(x): RngIntRes -> RngIntResElt, RngIntResElt"}
examples:
  - "ex-8ae5da"
  - "ex-bde8d2"
---

# The Unit Group

<a id="function-unitgroup-rngintres-2"></a>
## `UnitGroup(R): RngIntRes -> GrpAb, Map`

Given $R={{\mathbb{Z}}/m{\mathbb{Z}}}$, construct the unit group of $R$ as an abelian group. This returns an (additive) abelian group $A$ of order $\phi(m)$, together with a map from $A$ to $R$.

<a id="function-isprimitive-rngintreselt"></a>
## `IsPrimitive(n): RngIntResElt -> BoolElt`

Returns `true` if the element $n\in{{\mathbb{Z}}/m{\mathbb{Z}}}$ is primitive, that is, if it generates the multiplicative group of ${{\mathbb{Z}}/m{\mathbb{Z}}}$, `false` otherwise.

<a id="function-primitiveelement-rngintres"></a>
## `PrimitiveElement(R): RngIntRes -> RngIntResElt`

<a id="function-primitiveroot-rngintres"></a>
## `PrimitiveRoot(R): RngIntRes -> RngIntResElt`

Given $R = {\mathbb{Z}}/m{\mathbb{Z}}$, this function returns a generator for the group of units of $R$ if this group is cyclic, and returns $0$ otherwise. Thus a valid generator is only returned if $m = 2, 4, p^t$ or $2p^t$, with $p$ an odd prime and $t\geq 1$.

<a id="function-order-rngintreselt"></a>
## `Order(a): RngIntResElt -> RngIntElt`

Given an element $a$ belonging to ${{\mathbb{Z}}/m{\mathbb{Z}}}$, return the multiplicative order $k \geq 1$ of $a$ if $a$ is in the unit group ${({{\mathbb{Z}}/m{\mathbb{Z}}})^*}$, and zero if $a$ is not a unit.

<a id="function-normalize-rngintres"></a>
## `Normalize(x): RngIntRes -> RngIntResElt, RngIntResElt`

<a id="function-normalise-rngintres"></a>
## `Normalise(x): RngIntRes -> RngIntResElt, RngIntResElt`

Given an element $x\in R = {\mathbb{Z}}/m{\mathbb{Z}}$, this function returns the unique canonical associate $y\in R$ of $x$ and a unit $u\in R$ such that $u\cdot x = y$. The canonical associate of $x$ is the GCD of $x$ and $m$, considered as natural integers (unless $x$ is 0, in which case it is 0).

<a id="example-ex-8ae5da"></a>
## `Example: Unit Group of a Residue Class Ring (ex-8ae5da)`

We determine the unit group of the ring with modulus $735$ and then verify its order by comparing it with $\phi(m)$.

```magma
> m := 735;
> R := ResidueClassRing(m);
Residue class ring of integers modulo 735
> U, psi := UnitGroup(R);
> U;
Abelian Group isomorphic to Z/2 + Z/2 + Z/84
Defined on 3 generators
Relations:
    2*U.1 = 0
    4*U.2 = 0
    42*U.3 = 0
> #U;
336
> EulerPhi(735);
336
```

So the order of $U$ is equal to $\phi(m)$ as it should be. Finally, we look for three elements of $R$ that generate the unit group.

```magma
> gens := [ psi(U.i) : i in [1..3] ]; gens;
> [ Order(x) : x in gens ];
[ 2, 4, 42 ]
```

<a id="example-ex-bde8d2"></a>
## `Example: Cyclic Unit Group (ex-bde8d2)`

We construct a residue class ring $R = {\mathbb{Z}}/m{\mathbb{Z}}$ having cyclic unit group. By a theorem of Gauss, the ring $R$ has cyclic unit group precisely when $n = 4, n = p^e,$ or $n = 2p^e$, and $p$ is an odd prime.

```magma
> R := IntegerRing(50);
> U, psi := UnitGroup(R);
Abelian Group isomorphic to Z/20
Defined on 1 generator
Relations:
    20*U.1 = 0
> w := PrimitiveElement(R);
> w;
3
> Order(w);
20
```

We verify that the powers of $w$ are precisely the elements of the unit group $U$.

```magma
> powers := { w^i : i in [0..19] };
> powers;
{ 29, 1, 31, 3, 33, 7, 37, 9, 39, 11, 41, 13, 43, 17, 47, 19, 49, 21, 23, 27 }
> powers eq { psi(u) : u in U };
true
```
