示例#1
0
    #  ----------------------------------------
    #   999, 997  (-2 from maximum of 999,999)
    #   998, 998  (-2 from maximum of 999,999)
    #  ----------------------------------------
    #   999, 996  (-3 from maximum)
    #   998, 997  (-3 from maximum)
    #  ----------------------------------------
    #   999, 995  (-4 from maximum)
    #   998, 996  (-4 from maximum)
    #   997, 997  (-4 from maximum)
    # And so on.
    # This generates the list of all unique products, starting
    # with the largest. Similar to the "dovetailing" procedure
    # used to put the rationals in cardinality with the integers.
    # Then we simply test each one for product-palindromeness and
    # return when done.
    for m in range(2, 2000):
        for i in range(1, int(m / 2) + 1):
            a = 1000 - i
            b = 1000 - (m - i)
            if is_palindrome(a * b):
                # Note: We may be returning prematurely if a later "split" of
                # m produces multiplicands which multiply to a larger number.
                # Really, we should be doing a max() on SOMEthing
                return a, b, a * b


if __name__ == '__main__':
    import utils
    utils.solution_printer(ANSWER)
示例#2
0
文件: p75.py 项目: gmhorn/euler
    print('-' * 39)
    print('Count unique perimeters. L = 100000')
    print(
        timeit.timeit(
            'unique_pythagorean_perimeters(100000)',
            number=1,
            setup='from __main__ import unique_pythagorean_perimeters'))

    print('-' * 39)
    print('Count unique perimeters. L = 1000000')
    print(
        timeit.timeit(
            'unique_pythagorean_perimeters(1000000)',
            number=1,
            setup='from __main__ import unique_pythagorean_perimeters'))


def PROFILE():
    import cProfile
    cProfile.run('unique_pythagorean_perimeters(100000)')


def TEST():
    unittest.main(verbosity=2, exit=False)


if __name__ == '__main__':
    import utils
    utils.solution_printer(unique_pythagorean_perimeters, (1500000, ))
示例#3
0
文件: p87.py 项目: gmhorn/euler
    primes = list(utils.numtheory.bounded_soe(largest_sqrt))
    # Find the indicies of the next values greater than or equal to
    # largest_fourth, largest_third, and largest_sqrt in our list of
    # primes
    fourth_index = bisect.bisect_left(primes, largest_fourth)
    third_index = bisect.bisect_left(primes, largest_third)
    sqrt_index = bisect.bisect_left(primes, largest_sqrt)
    # Create a set of vals
    calculated_vals = set()
    for fourth in primes[:fourth_index+1]:
        s4 = power(fourth, 4)
        for third in primes[:third_index+1]:
            s3 = s4 + power(third, 3)
            if s3 >= N: break
            for sqrt in primes[:sqrt_index+1]:
                s = s3 + power(sqrt, 2)
                if s <= N:
                    calculated_vals.add(s)
                else:
                    break
    return len(calculated_vals)

def PROFILE():
    import cProfile
    cProfile.run('sums_below(5e7)')

if __name__ == '__main__':
    import utils
    #PROFILE()
    utils.solution_printer(sums_below, (5e7,))
示例#4
0
            # the factor sum to give N:
            # For N=12 and fact_sum=(3+2+2)=7, we calculate num_ones=12-7=5
            num_ones = N - fact_sum
            # So for our example, we need 5 1's to make the sum and product
            # equal 12.
            # 12 = 1+1+1+1+1+3+2+2 = 1*1*1*1*1*3*2*2
            # So in this case, k = len(1,1,1,1,1,3,2,2)
            #                    = num_ones + len(factors)
            k = num_ones + len(factors)
            # If this k is in our elements_to_go set, we know that this N is
            # the smallest product-sum number for that k. So update our answer
            # dict and remove k from our elements to go
            if k in elements_to_go:
                N_mins[k] = (N, factors+[1]*num_ones)
                elements_to_go.remove(k)
    return N_mins

def sum_of_minimal_product_sum_numbers(kmin, kmax):
    d = min_product_sum_numbers(kmin, kmax)
    vals = [v[0] for v in d.values()]
    return sum(set(vals))

def PROFILE():
    import cProfile
    cProfile.run('sum_of_minimal_product_sum_numbers(2, 5000)')

if __name__ == '__main__':
    import utils
    utils.solution_printer(sum_of_minimal_product_sum_numbers,
                                 (2, 12000))