# How to generate the cosets in this specific group (Zn*)

I see the answers to the multiplicative group of units (a class), in here, as well as the answer to how to generate cosets in here.

But I don't know how to blend them together into a chunk of code that illustrates the cosets when the group is partitioned by the subgroup generated, for example, by the element 5 in the multiplicative group mod 13:

```
Zn = Zmod(13)
G = Zn.unit_group()
H = G.subgroup(5)
H.cosets()
```

A possible workaround after the comment below could be as follows:

```
def cosets(p,g):
R = Integers(p)
a = R(g)
sub = uniq([a^i for i in range(p - 1)])
diff = list(set(R) - set(sub))
m = []
for k in range(1, len(diff)): m.append(sorted(set([diff[k] * i for i in sub])))
costs = list(set(tuple(map(tuple, m))))
print("The elements of the multiplicative subgroup generated by", g, "modulo", p, "are", sub, "and the cosets are", costs)
```

So the call `cosets(13,5)`

would yield:

`The elements of the multiplicative subgroup generated by 5 modulo 13 are [1, 5, 8, 12] and the cosets are [(2, 3, 10, 11), (4, 6, 7, 9)]`

Would this work?

There are two issues here - first getting a subgroup defined properly (your code fails in that), and then computing the posets. The unit group is tricky to work on (e.g. see this answer) as there is no easy coercion from the parent group. Here is a workaround:

However, it's still troublesome to compute cosets as

`G.cosets(H)`

gives an error:When I run the code in your comment I get

`Multiplicative Abelian subgroup isomorphic to C4 generated by {f^9}`

Yes, the subgroup is constructed fine. There still remains a problem with computing cosets.

@Max Alekseyev isomorphic to C4 makes sense, by why

`generated by f^9`

?`f`

(generator of`G`

) happens to be chosen as 2 and we have`f^9`

equal 5 in`G`

.