def count_factors(n): """ Counts the total number of divisors/factors of the provided number. Here's the gist from http://mathforum.org/library/drmath/view/55843.html: A number n can be represented as: n = x^a + y^b + z^c + ... where x, y, z ... are the prime factors of n, and a, b, c, ... are their multiplicities. The number of total divisors of n = (a+1) * (b+1) * (c+1) * ... """ # builds a dict-like Counter object, where the keys are the distinct prime factors, and the # values are the multiplicity of that factor (aka, how many of that distinct factor were # present in the prime factorization) prime_factor_counter = Counter(prime_factors(n)) # get a list of the multiplicities of the prime factors + 1 prime_factor_multiplicities_plus_one = [(x + 1) for _, x in prime_factor_counter.most_common()] # multiply those all together and return multiply = lambda x, y: x * y return reduce(multiply, prime_factor_multiplicities_plus_one, 1)
def test_example(self): """ Check the provided example. """ self.assertEqual([i for i in prime_factors(13195)], [5, 7, 13, 29])
def problem003(output, argument=600851475143): """ Solve Problem #3. """ output.put((3, max(prime_factors(argument))))
"""Problem 3: Largest prime factor. Compute prime factors by continually dividing out smallest factor.""" import unittest from utils.primes import prime_factors if __name__ == "__main__": print(max(prime_factors(600851475143))) unittest.main()
#!/usr/bin/python from utils.primes import prime_factors print max(prime_factors(600851475143))
def problem_3(): print(max(prime_factors(600851475143)))