# Easiest way to work in the multiplicative group of Zmod(n)

Given an integer $n$, one can define in SageMath the additive group $\mathbb Z/n\mathbb Z$ by

sage: Zn = Zmod(n) # or Integers(n)


Now, I would like to work in the multiplicative group $(\mathbb Z/n\mathbb Z)^*$. Of course, I can write

sage: G = [a for a in Zn if gcd(a,n) == 1]
sage: Zn(4).multiplicative_order()
6


What I would like is an easier way of writing such a thing, such as:

sage: G = Zn.multiplicative_group() # does not exist!
sage: G(4).order()
6


Does there exist something in SageMath to perform such kind of computations?

edit retag close merge delete

Sort by » oldest newest most voted

You can use the unit group of your ring:

sage: n = 10
sage: Zn = Zmod(n)
sage: G = Zn.unit_group()
sage: G
Multiplicative Abelian group isomorphic to C4
sage: G.order()
4
sage: G.inject_variables()
Defining f
sage: f^2
f^2
sage: f.order()
4
sage: (f^2).order()
2


This group G comes with and embedding into Zn:

sage: Zn(f)
7
sage: Zn(f^2)
9
sage: Zn(f^3)
3
sage: Zn(f^4)
1
sage: Zn(f^5)
7

more

Do you think there needs to be better documentation in Zmod() of this? Maybe a note about multiplicative group? Or an alias for tab-completion? Just wondering what you think.

( 2015-09-28 20:53:46 +0200 )edit
1

Apparently I am not the only one who did not find that unit_group is the right method for this (cf this sage-devel thread). Another point: Though I understand that "the" G defined by unit_group is indeed the multiplicative group of Zn, I am still disturbed by the fact that its elements are not represented as integers. In particular, this implies that G(4) does not work, not G(Zn(4)).

( 2015-09-29 10:57:25 +0200 )edit

In your example, 4 is not invertible in $\mathbb{Z}/10\mathbb{Z}$, so the map is not well defined. However you can go on the other way, from G to Zn. I edited the end of my answer to give an example.

( 2015-09-29 15:33:09 +0200 )edit

Right, I chose a bad example, though of course it does not work with G(7) either. Well, thanks for the answer and its edition!

( 2015-09-29 17:32:45 +0200 )edit
( 2015-10-01 20:54:55 +0200 )edit