#!/usr/bin/env python3

from Cryptotools.Utils.utils import gcd


def carmi_first_method(n) -> int:
    coprimes = list()
    for i in range(1, n):
        if gcd(i, n) == 1:
            coprimes.append(i)
    print(coprimes) 
    smallest = list()
    for m in range(1, n):
        l = 0
        for a in coprimes:
            if a ** m % n == 1:
                l += 1
        if l == len(coprimes):
            smallest.append(m)
    
    #print(smallest)
    return min(smallest)

print(f"lambda(12) = {carmi_first_method(12)}")
print(f"lambda(19) = {carmi_first_method(19)}")
#print(f"lambda(28) = {carmi_first_method(28)}")
#print(f"lambda(32) = {carmi_first_method(32)}")
#print(f"lambda(33) = {carmi_first_method(33)}")
#print(f"lambda(35) = {carmi_first_method(35)}")
#print(f"lambda(36) = {carmi_first_method(36)}")
#print(f"lambda(135) = {carmi_first_method(135)}")
#print(f"lambda(1200) = {carmi_first_method(1200)}")
