def refined_brute_force_solution(): """Use faster prime factorization to help reduce series numbers which must be factored.""" series_number = 0 for i in itertools.count(1): # The total number of factors of a number must be less than or equal to the total # combinations of all of prime factors of that number. The number of combinations of a set # is equal to 2^N where N is the size of the set so we can quickly compute whether that # number is at least NUM_FACTORS prime_factors = utils.get_prime_factorization(series_number) if 2**len(prime_factors) > NUM_FACTORS: if len(utils.get_factors(series_number)) > NUM_FACTORS: break series_number += i return series_number
def lcm(numbers): """ Compute the least common multiple (LCM) of the provided list of numbers. If the prime factorization of one number is contained within the set of all prime factors then that number is already a multiple of the number that is multiple of all prime factors. """ lcm_prime_factors = {} for i in numbers: i_prime_factors = collections.Counter(utils.get_prime_factorization(i)) for key in i_prime_factors: if i_prime_factors[key] > lcm_prime_factors.get(key, 0): lcm_prime_factors[key] = i_prime_factors[key] return counter_product(lcm_prime_factors)
def sum_of_divisors(number): """ Return the sum of divisors for the provided number. To find the sum of divisors S(num), first find the prime factorization of the number: S(num) = S(a^m * b^n ...) where a^m * b^n ... is the prime factorization S(a^m * b^n ...) can then be written as S(a^m) * S(b^n) * ... To find the sum of divisors for a number which can be described as p^k: S(p^k) = (p^(k+1) - 1) / (p-1) """ prime_factorization = utils.get_prime_factorization(number) def divisor_sum_of_prime(prime, count): return ((prime**(count + 1)) - 1) / (prime - 1) return int( functools.reduce(operator.mul, [ divisor_sum_of_prime(prime, prime_factorization.count(prime)) for prime in set(prime_factorization) ]))
def has_distinct_prime_factors(value, num_prime_factors): """Return True if value has distinct num_prime_factors.""" return len(set(utils.get_prime_factorization(value))) == num_prime_factors
def largest_prime_factor_fast(): return utils.get_prime_factorization(TEST_NUMBER)[-1]