示例#1
0
文件: p06x.py 项目: jpaeng/PE
def min_prime_pair_set(prime_count):
    if prime_count < 5:
        prime_list = common.sieve_erathosthenes2(10**(2*prime_count))
        min_prime_sum = 10**prime_count
    else:
        prime_list = common.sieve_erathosthenes2(10**8)
        min_prime_sum = 10**8

    min_prime_set = []

    prime_index = 1     # No pair with 2 will work
    while prime_list[prime_index] < min_prime_sum//prime_count:
        prime_set = [prime_list[prime_index]]
        prime_sum = prime_set[0]
        next_prime_index = prime_index + 1
        while prime_list[next_prime_index]+prime_sum < min_prime_sum:
            if check_prime_pairs(next_prime_index, prime_set, prime_list):
                prime_set.append(prime_list[next_prime_index])
                prime_sum = sum(prime_set)
                # if len(prime_set) >= 4:
                    # print('    ', len(prime_set), prime_set)
                if prime_sum > min_prime_sum:
                    break
                if len(prime_set) == prime_count:
                    if prime_sum < min_prime_sum:
                        min_prime_set = prime_set[:]
                        min_prime_sum = prime_sum
                    break
            next_prime_index += 1
        prime_index += 1

    return min_prime_set, min_prime_sum
示例#2
0
文件: test_pe.py 项目: jpaeng/PE
def test_common():
    # variables
    ordered_list = list(range(-10, 10))
    prime_list_10 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    # power_digit_sum
    common.power_digit_sum(2, 15) == 26

    # index_in_ordered_list
    assert common.index_in_ordered_list(-10, ordered_list) == ordered_list.index(-10)
    assert common.index_in_ordered_list( -1, ordered_list) == ordered_list.index(-1)
    assert common.index_in_ordered_list(  0, ordered_list) == ordered_list.index(0)
    assert common.index_in_ordered_list(  9, ordered_list) == ordered_list.index(9)
    assert common.index_in_ordered_list( 10, ordered_list) == -1
    assert common.index_in_ordered_list(-11, ordered_list) == -1

    # is_in_ordered_list
    assert common.is_in_ordered_list(-10, ordered_list)
    assert common.is_in_ordered_list( -1, ordered_list)
    assert common.is_in_ordered_list(  0, ordered_list)
    assert common.is_in_ordered_list(  9, ordered_list)
    assert common.is_in_ordered_list( 10, ordered_list) is False
    assert common.is_in_ordered_list(-11, ordered_list) is False

    assert common.str_permutation(11, '0123') == '1320'
    assert common.str_permutation(999999, '0123456789') == '2783915460'

    # get_factors
    assert common.get_factors(1) == [1]
    assert common.get_factors(16) == [1, 2, 4, 8, 16]

    # sieve_erathosthenes
    assert common.sieve_erathosthenes(30) == prime_list_10
    assert common.sieve_erathosthenes(30) != ordered_list
    assert common.sieve_erathosthenes2(30) == prime_list_10
    assert common.prime_list_mr(0, 30) == prime_list_10

    # get_prime_factors
    assert common.get_prime_factors(0, prime_list_10) == []
    assert common.get_prime_factors(2, prime_list_10) == [2]
    assert common.get_prime_factors(512, prime_list_10) == [2]
    assert common.get_prime_factors(60, prime_list_10) == [2, 3, 5]
    assert common.get_prime_factors(6469693230, prime_list_10) == prime_list_10