def carmichael_lambda(n): """Returns the smallest positive integer m such that a**m = 1 (mod n) for all integers a coprime to n. Parameters * n: int or list (n > 1) The modulus. This value can be an int or a factorization. Returns: * m: int Raises: * ValueError: If n <= 0 or n is not an integer or factorization. Examples: >>> carmichael_lambda(100) 20 >>> carmichael_lambda([(2, 2), (5, 2)]) 20 >>> carmichael_lambda(113) 112 >>> carmichael_lambda(0) Traceback (most recent call last): ... ValueError: carmichael_lambda: Must have n > 0. Details: The Carmichael lambda function gives the exponent of the multiplicative group of integers modulo n. For each n, lambda(n) divides euler_phi(n). This method uses the standard definition of the Carmichael lambda function. In particular, lambda(n) is the least common multiple of the values of lambda(p**e) for each prime power in the factorization of n. See "Fundamental Number Theory with Applications" by Mollin for details. """ if isinstance(n, (int, long)): if n == 1: return 1 elif n <= 0: raise ValueError("carmichael_lambda: Must have n > 0.") n_factorization = factor.factor(n) elif isinstance(n, list) and n[0][0] > 0: n_factorization = n else: raise ValueError("carmichael_lambda: Input must be a positive integer or a factorization.") terms = [] for (p, e) in n_factorization: if p != 2: term = p**(e - 1)*(p - 1) else: if e <= 2: term = 1 << (e - 1) else: term = 1 << (e - 2) terms.append(term) value = lcm_list(terms) return value
def carmichael_lambda(n): r"""Returns the value of the Carmichael lambda function at `n`. This function returns the smallest positive integer `m` such that ``a**m = 1 (mod n)`` for all integers `a` coprime to `n`. This integer `m` is also called the least universal exponent for `n`. Parameters ---------- n : integer or factorization (n > 1) The value of `n` can be an integer or the factorization of an integer given as a list of (prime, exponent) pairs. Returns ------- m : int The least universal exponent for `n`. Raises ------ ValueError If ``n <= 0`` or if `n` is a valid factorization. Notes ----- The Carmichael lambda function gives the exponent of the multiplicative group of integers modulo n. For each n, lambda(n) divides euler_phi(n). This method uses the standard definition of the Carmichael lambda function. In particular, lambda(n) is the least common multiple of the values of lambda(p**e) for each prime power in the factorization of n. See "Fundamental Number Theory with Applications" by Mollin for details. Examples -------- >>> carmichael_lambda(100) 20 >>> carmichael_lambda([(2, 2), (5, 2)]) 20 >>> carmichael_lambda(113) 112 >>> carmichael_lambda(0) Traceback (most recent call last): ... ValueError: carmichael_lambda: Must have n > 0. """ if isinstance(n, (int, long)): if n == 1: return 1 elif n <= 0: raise ValueError("carmichael_lambda: Must have n > 0.") n_factorization = rosemary.number_theory.factorization.factorization.factor( n) elif isinstance(n, list) and n[0][0] > 0: n_factorization = n else: raise ValueError( "carmichael_lambda: Input must be a positive integer or a factorization." ) terms = [] for (p, e) in n_factorization: if p != 2: term = p**(e - 1) * (p - 1) else: if e <= 2: term = 1 << (e - 1) else: term = 1 << (e - 2) terms.append(term) value = lcm_list(terms) return value
def test_lcm_list(self): self.assertEqual(lcm_list(range(1, 31)), 2329089562800)