コード例 #1
0
 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)
コード例 #2
0
    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.
        """

        up_to = 0
        delta = 5000
        while len(Arithmetic.PRIMES_LIST) < 10002:
            up_to += delta
            Arithmetic.primes_up_to(up_to)

        return Arithmetic.PRIMES_LIST[10000]
コード例 #3
0
 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.
     """
     
     up_to = 0
     delta = 50000
     while up_to < 2000000:
         up_to += delta
         Arithmetic.primes_up_to(up_to)
     
     p_list = Arithmetic.primes_up_to(2000000)
     return "%d" % np.sum(p_list)
コード例 #4
0
 def test_set_position(self):
     
     values = Arithmetic.primes_up_to(2)
     msg = 'Primes through 2'
     exp = np.array([2])
     self.assertListEqual(list(exp), list(values), msg)
     
     values = Arithmetic.primes_up_to(6)
     msg = 'Primes through 6'
     exp = np.array([2, 3, 5])
     self.assertListEqual(list(exp), list(values), msg)
     
     values = Arithmetic.primes_up_to(20)
     msg = 'Primes through 6'
     exp = np.array([2, 3, 5, 7, 11, 13, 17, 19])
     self.assertListEqual(list(exp), list(values), msg)
コード例 #5
0
 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)
コード例 #6
0
 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]
コード例 #7
0
 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.
     """
     # Initialize the palindrome instance with a palindrome just
     # above the largest possible and walk backwards.
     #
     # 999 * 999 = 998001  so 998899 is too large
     p_value = 998899
     p_seq = Palindrome(p_value)
     
     p_list = Arithmetic.primes_up_to(1000)
     
     while p_value >= 10000:
         p_value = p_seq.previous()
         factors = np.array([])
         value = p_value
         for prime in p_list:
             # If the value is 1, we've got it all
             if value == 1:
                 break
             while value % prime == 0:
                 # this prime is a factor of value so pull it out
                 factors = np.append(factors, prime)
                 value = value / prime
         if value != 1:
             # If the value is not 1 by now, a larger than 3 digit
             # prime factor exists so this is not a solution.
             continue
         
         # Try to construct two 3-digit factors.  Start at the highest
         # prime factor, and multiply it in to the lowest of the two 
         # factors as you go trying to keep them even so both end up
         # under the 1000 mark.
         factor_1 = 1
         factor_2 = 1
         for index in range(len(factors), 0, -1):
             factor = factors[index-1]
             if factor_1 <= factor_2:
                 factor_1 *= factor
             else:
                 factor_2 *= factor
         if factor_1 < 1000 and factor_2 < 1000:
             break
     
     return '%d x %d = %d' % (factor_1, factor_2, factor_1 * factor_2)
コード例 #8
0
 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.
     """
     
     tri_obj = TriangleNumbers()
     
     while True:
         value = tri_obj.next()
         factor_count = Arithmetic.factors(value, True)
         if factor_count > 500:
             break
     
     return value