def test_prime_factorization(self): values = Arithmetic.prime_factorization(1) msg = 'Prime factorization for 1' exp = np.array([]) self.assertListEqual(list(exp), list(values), msg) values = Arithmetic.prime_factorization(3) msg = 'Prime factorization for 3' exp = np.array([3]) self.assertListEqual(list(exp), list(values), msg) values = Arithmetic.prime_factorization(6) msg = 'Prime factorization for 3' exp = np.array([2, 3]) self.assertListEqual(list(exp), list(values), msg) values = Arithmetic.prime_factorization(10) msg = 'Prime factorization for 3' exp = np.array([2, 5]) self.assertListEqual(list(exp), list(values), msg) values = Arithmetic.prime_factorization(15) msg = 'Prime factorization for 3' exp = np.array([3, 5]) self.assertListEqual(list(exp), list(values), msg)
def find_solution(self): """ This method contains the guts of finding the solution. The timer starts just prior to calling this method and stops just after returning the solution's value. """ # What number to we want to include up to. up_to = 20 # Initialize the prime factors for the solution factors = np.array([]) # Starting at 1 and going up to the up_to limit, # Calculate the prime factorization for each item # If the facotrs have already been accounted for, we're # all set. # # Ex: from 2 to 7 the factors array would include # [ 2, 2, 3, 5, 7 ] # The prime factorization for 8 is 2 * 2 * 2. # The factors array already has 2's but not three # of them so add one more. # [ 2, 2, 2, 3, 5, 7 ] for item in range(2, up_to + 1): # Get the prime factorization for "item" primes = Arithmetic.prime_factorization(item) for factor in primes: # if the factor is not present or there are not enough # of them append another one on if sum(factors == factor) < sum(primes == factor): factors = np.append(factors, factor) # Final answer is the product of the accumulated primes return np.prod(factors)
def find_solution(self): """ This method contains the guts of finding the solution. The timer starts just prior to calling this method and stops just after returning the solution's value. """ value = 600851475143 factors = Arithmetic.prime_factorization(value) if not np.prod(factors) == value: print 'ERROR' return "%d" % factors[-1]