for positive integer n and given integer a that satisfies gcda n 1 the order of a modulo n is the smallest positive integer k that satisfies pow a k n 1 in other words ak 1 mod n order of a certain number may or may not be exist if not return 1 total time complexity onlogn on for iteration loop ologn for builtin power function find order for positive integer n and given integer a that satisfies gcda n 1 exception handeling 1 is the order of of 1 find order for positive integer n and given integer a that satisfies gcd a n 1 exception handeling 1 is the order of of 1
import math

def find_order(a, n):
    
    if (a == 1) & (n == 1):
        
        return 1
    if math.gcd(a, n) != 1:
        print ("a and n should be relative prime!")
        return -1
    for i in range(1, n):
        if pow(a, i) % n == 1:
            return i
    return -1