(p, n)
return m
Find the greatest integer m such that p**m divides n.

    Examples
    ========

    >>> from sympy.ntheory import multiplicity
    >>> from sympy.core.numbers import Rational as R
    >>> [multiplicity(5, n) for n in [8, 5, 25, 125, 250]]
    [0, 1, 2, 3, 3]
    >>> multiplicity(3, R(1, 9))
    -2
Find the greatest integer m such that p**m divides n.
Find the greatest integer m such that p ** m divides n .
def multiplicity(p, n):
    """
    Find the greatest integer m such that p**m divides n.

    Examples
    ========

    >>> from sympy.ntheory import multiplicity
    >>> from sympy.core.numbers import Rational as R
    >>> [multiplicity(5, n) for n in [8, 5, 25, 125, 250]]
    [0, 1, 2, 3, 3]
    >>> multiplicity(3, R(1, 9))
    -2

    """
    try:
        p, n = as_int(p), as_int(n)
    except ValueError:
        if all(isinstance(i, (SYMPY_INTS, Rational)) for i in (p, n)):
            try:
                p = Rational(p)
                n = Rational(n)
                if p.q == 1:
                    if n.p == 1:
                        return -multiplicity(p.p, n.q)
                    return S.Zero
                elif p.p == 1:
                    return multiplicity(p.q, n.q)
                else:
                    like = min(
                        multiplicity(p.p, n.p),
                        multiplicity(p.q, n.q))
                    cross = min(
                        multiplicity(p.q, n.p),
                        multiplicity(p.p, n.q))
                    return like - cross
            except AttributeError:
                pass
        raise ValueError('expecting ints or fractions, got %s and %s' % (p, n))

    if p == 2:
        return trailing(n)
    if p < 2:
        raise ValueError('p must be an integer, 2 or larger, but got %s' % p)
    if p == n:
        return 1

    m = 0
    n, rem = divmod(n, p)
    while not rem:
        m += 1
        if m > 5:
            # The multiplicity could be very large. Better
            # to increment in powers of two
            e = 2
            while 1:
                ppow = p**e
                if ppow < n:
                    nnew, rem = divmod(n, ppow)
                    if not rem:
                        m += e
                        e *= 2
                        n = nnew
                        continue
                return m + multiplicity(p, n)
        n, rem = divmod(n, p)
    return m
def multiplicity ( p , n ) : try : p , n = as_int ( p ) , as_int ( n ) except ValueError : if all ( isinstance ( i , ( SYMPY_INTS , Rational ) ) for i in ( p , n ) ) : try : p = Rational ( p ) n = Rational ( n ) if p . q == 1 : if n . p == 1 : return - multiplicity ( p . p , n . q ) return S . Zero elif p . p == 1 : return multiplicity ( p . q , n . q ) else : like = min ( multiplicity ( p . p , n . p ) , multiplicity ( p . q , n . q ) ) cross = min ( multiplicity ( p . q , n . p ) , multiplicity ( p . p , n . q ) ) return like - cross except AttributeError : pass raise ValueError ( 'expecting ints or fractions, got %s and %s' % ( p , n ) ) if p == 2 : return trailing ( n ) if p < 2 : raise ValueError ( 'p must be an integer, 2 or larger, but got %s' % p ) if p == n : return 1 m = 0 n , rem = divmod ( n , p ) while not rem : m += 1 if m > 5 : # The multiplicity could be very large. Better # to increment in powers of two e = 2 while 1 : ppow = p ** e if ppow < n : nnew , rem = divmod ( n , ppow ) if not rem : m += e e *= 2 n = nnew continue return m + multiplicity ( p , n ) n , rem = divmod ( n , p ) return m