예제 #1
0
def test_consecutive_primes(n2, xs):
    # First the values themselves should be primes.
    if not all(test_primality(n2 + x) for x in xs):
        return False
    # There should be no other primes besides those specified in x.
    max_x = max(xs)
    for x in xrange(xs[0] + 2, max_x, 2):
        if x in xs:
            continue
        if test_primality(n2 + x):
            return False
    return True
예제 #2
0
def solve_p130(num, verbose=True):
    s = 0
    count = 0
    # 9 is the first odd composite value.
    v = 9
    while count < num:
        # Check only composite values
        if not test_primality(v):
            digits = least_repunit(v)
            if (v - 1) % digits == 0:
                s += v
                count += 1
                if verbose:
                    print v
        # Only need to consider 1, 3, 7, and 9, so increment by two.
        v += 2
        # Skip multiples of 5 manually.
        if v % 5 == 0:
            v += 2
    return s
예제 #3
0
def test_positions(base, swap_digits):
    # No digits left to test, simply test primality
    if len(swap_digits) == 0:
        # If 0 is in the first digit, reject it since it no longer counts.
        if base[0] == 0:
            return 0, 0
        # Convert from a list of digits to a number of values.
        val = convert_list_number(base)
        if test_primality(val):
            return 1, val
        else:
            return 0, 0
    # Test all values for the first digit, recursively try the rest.
    first = swap_digits[0]
    remain = swap_digits[1:]
    count = 0
    sum_vals = 0
    for possible in range(10):
        base[first] = possible
        partial_count, partial_sum = test_positions(base, remain)
        count += partial_count
        sum_vals += partial_sum
    return count, sum_vals
예제 #4
0
def is_prime(v):
    return test_primality(v)
예제 #5
0
def is_prime(x):
    return test_primality(x)