def content(self): """ Returns the content of self. The content of a polynomial with integral coefficients is defined to be the greatest common divisor of the coefficients. """ result = gcd_list(self._coefficients) return result
def primitive_part(self): """ Returns the primitive part of self. The primitive part of a polynomial with integral coefficients is defined to be the polynomial obtained by dividing out the content of self. It is conventional to define the primitive part so that its leading coefficient is positive. """ coeffs = self._coefficients content = gcd_list(coeffs) if coeffs[-1] < 0: content *= -1 parent_class = self.__class__ primitive_part_coeffs = [coeff // content for coeff in coeffs] result = parent_class(primitive_part_coeffs, self.variable) return result
def primitive_part(self): """ Returns the primitive part of self. The primitive part of a polynomial with integral coefficients is defined to be the polynomial obtained by dividing out the content of self. It is conventional to define the primitive part so that its leading coefficient is positive. """ coeffs = self._coefficients content = gcd_list(coeffs) if coeffs[-1] < 0: content *= -1 parent_class = self.__class__ primitive_part_coeffs = [coeff//content for coeff in coeffs] result = parent_class(primitive_part_coeffs, self.variable) return result
def is_power(n, k=None, detect_only=False): """Determines if n is a perfect power. If n is a perfect power, this returns (b, k) where n = b^k with k maximal. If the optional parameter k is given, this returns (b, k) if n = b^k for some b. Otherwise, returns False. Input: * n: int or list (n >= 1) The value of n can be an int or a factorization. * k: int (k >= 1) (default=None) Returns: * (b, k): tuple of ints Values such that n = b^k if such values exist. * Returns False if no such values exist. Raises: * ValueError: If n <= 0 or k <= 0. Examples: >>> is_power(36) (6, 2) >>> is_power(81) (3, 4) >>> is_power(81, 2) (9, 2) >>> is_power(1330) False >>> is_power([(2, 4), (5, 4)]) (10, 4) >>> is_power([(2, 4), (5, 4)], 2) (100, 2) >>> is_power([(2, 3), (5, 2)]) False >>> is_power(17, 0) Traceback (most recent call last): ... ValueError: is_power: Must have k >= 1. >>> is_power(0) Traceback (most recent call last): ... ValueError: is_power: Must have n >= 1. Details: If n is given as an int and a value of k is given, then this function computes the integer kth root r of n and checks if r^k = n. In the case where no such k is given, this looks at each k >= 2, extracts the integer kth root of n, and uses this to determine if n is a kth power. If n is given as a factorization and a value of k is given, this function checks if each exponent in the prime factorization of n is divisible by k. If no such k is given, this computes the gcd d of the exponents in the prime factorization and checks if d > 1. """ if k is not None and k < 1: raise ValueError("is_power: Must have k >= 1.") if isinstance(n, list): if n[0][0] == -1: raise ValueError("is_power: Must have n >= 1.") n_factorization = n[:] kth_root = 1 if k is not None: for (p, e) in n_factorization: if e % k == 0: kth_root *= p**(e // k) else: return False return (kth_root, k) else: exponents = [e for (p, e) in n_factorization] d = gcd_list(exponents) if d == 1: return False else: kth_root = 1 for (p, e) in n_factorization: kth_root *= p**(e // d) return (kth_root, d) else: if n < 1: raise ValueError("is_power: Must have n >= 1.") if k is not None: if k == 1: return (n, 1) root = integer_nth_root(k, n) if root**k == n: return (root, k) return False for k in xrange(2, n.bit_length() + 1): base = integer_nth_root(k, n) if base**k == n: exponent = k while True: next_step = is_power(base) if next_step: base = next_step[0] exponent *= next_step[1] else: return (base, exponent) return False
def test_gcd_list(self): self.assertEqual(gcd_list([2, 4, 6, 8]), 2) self.assertEqual(gcd_list([1, 4, 6, 8]), 1) self.assertEqual(gcd_list([-2, 4, 6, 8]), 2) self.assertEqual(gcd_list([0, 4, 6, 8]), 2)
def test_gcd_list(self): self.assertEqual(gcd_list([2, 4, 6, 8]), 2) self.assertEqual(gcd_list([1, 4, 6, 8]), 1) self.assertEqual(gcd_list([-2, 4, 6, 8]), 2) self.assertEqual(gcd_list([0, 4, 6, 8]), 2) self.assertEqual(gcd_list([2]), 2)