(a, n)
return order
Returns the order of ``a`` modulo ``n``.

    The order of ``a`` modulo ``n`` is the smallest integer
    ``k`` such that ``a**k`` leaves a remainder of 1 with ``n``.

    Examples
    ========

    >>> from sympy.ntheory import n_order
    >>> n_order(3, 7)
    6
    >>> n_order(4, 7)
    3
Returns the order of ``a`` modulo ``n``.
Returns the order of a modulo n .
def n_order(a, n):
    """Returns the order of ``a`` modulo ``n``.

    The order of ``a`` modulo ``n`` is the smallest integer
    ``k`` such that ``a**k`` leaves a remainder of 1 with ``n``.

    Examples
    ========

    >>> from sympy.ntheory import n_order
    >>> n_order(3, 7)
    6
    >>> n_order(4, 7)
    3
    """
    from collections import defaultdict
    a, n = as_int(a), as_int(n)
    if igcd(a, n) != 1:
        raise ValueError("The two numbers should be relatively prime")
    factors = defaultdict(int)
    f = factorint(n)
    for px, kx in f.items():
        if kx > 1:
            factors[px] += kx - 1
        fpx = factorint(px - 1)
        for py, ky in fpx.items():
            factors[py] += ky
    group_order = 1
    for px, kx in factors.items():
        group_order *= px**kx
    order = 1
    if a > n:
        a = a % n
    for p, e in factors.items():
        exponent = group_order
        for f in xrange(e + 1):
            if pow(a, exponent, n) != 1:
                order *= p ** (e - f + 1)
                break
            exponent = exponent // p
    return order
def n_order ( a , n ) : from collections import defaultdict a , n = as_int ( a ) , as_int ( n ) if igcd ( a , n ) != 1 : raise ValueError ( "The two numbers should be relatively prime" ) factors = defaultdict ( int ) f = factorint ( n ) for px , kx in f . items ( ) : if kx > 1 : factors [ px ] += kx - 1 fpx = factorint ( px - 1 ) for py , ky in fpx . items ( ) : factors [ py ] += ky group_order = 1 for px , kx in factors . items ( ) : group_order *= px ** kx order = 1 if a > n : a = a % n for p , e in factors . items ( ) : exponent = group_order for f in xrange ( e + 1 ) : if pow ( a , exponent , n ) != 1 : order *= p ** ( e - f + 1 ) break exponent = exponent // p return order