from math import gcd
def carmi(n):
copr = [x for x in range(1, n) if gcd(x, n) == 1]
k = 1
while not all(pow(x, k, n) == 1 for x in copr):
k += 1
return k
If you get an import error on line 1, change math
to fractions
and see how it works