Example #1
0
File: p03x.py Project: jpaeng/PE
def integral_right_triangle_perimeters(max_p):
    max_n = int(max_p/2)
    squares = [n*n for n in range(max_n+1)]
    perimeters_list = []
    for a in range(1, max_n+1):
        for b in range(a, max_n+1):
            c = common.index_in_ordered_list(squares[a]+squares[b], squares)
            if c > 0:
                p = a + b + c
                if p > max_p:
                    break
                else:
                    perimeters_list.append(p)
    return perimeters_list
Example #2
0
File: test_pe.py Project: 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