def multiplicity(p, n): """ Find the greatest integer m such that p**m divides n. Examples ======== >>> from sympy.ntheory import multiplicity >>> [multiplicity(5, n) for n in [8, 5, 25, 125, 250]] [0, 1, 2, 3, 3] """ from residue_ntheory import int_tested p, n = int_tested(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 totient(n): """ Calculate the Euler totient function phi(n) >>> from sympy.ntheory import totient >>> totient(1) 1 >>> totient(25) 20 See Also ======== divisor_count """ from residue_ntheory import int_tested n = int_tested(n) if n < 1: raise ValueError("n must be a positive integer") factors = factorint(n) t = 1 for p, k in factors.iteritems(): t *= (p - 1) * p**(k - 1) return t
def totient(n): """ Calculate the Euler totient function phi(n) >>> from sympy.ntheory import totient >>> totient(1) 1 >>> totient(25) 20 See Also ======== divisor_count """ from residue_ntheory import int_tested n = int_tested(n) if n < 1: raise ValueError("n must be a positive integer") factors = factorint(n) t = 1 for p, k in factors.iteritems(): t *= (p-1) * p**(k-1) return t