Пример #1
0
def search(goal):
    """
    >>> search(6)
    13
    >>> search(7)
    56003
    """
    already_searched = set()
    for prime in primes():
        if prime in already_searched:
            continue
        positions_list = list(position_combinations(prime))
        for positions in positions_list:
            digits = list(digits_of(prime))
            family = set()
            for digit in range(10):
                if 0 in positions and digit == 0:
                    continue
                for pos in positions:
                    digits[pos] = digit
                number = from_digits(digits)
                if is_prime(number):
                    already_searched.add(number)
                    family.add(number)
            if len(family) >= goal:
                return min(family)
Пример #2
0
def cancel_unorthodox(numerator, denominator):
    """
    >>> cancel_unorthodox(49, 98)
    (4, 8)
    >>> cancel_unorthodox(499, 998)
    (4, 8)
    >>> cancel_unorthodox(11, 12)
    (1, 2)
    """
    n_digits = digits_of(numerator)
    d_digits = digits_of(denominator)
    matching_digits = [d for d in n_digits if d in d_digits]
    for m in matching_digits:
        if m in n_digits and m in d_digits:
            n_digits.remove(m)
            d_digits.remove(m)
    return from_digits(n_digits), from_digits(d_digits)
Пример #3
0
def reverse_and_add(n):
    """
    >>> reverse_and_add(47)
    121
    >>> reverse_and_add(349)
    1292
    >>> reverse_and_add(1292)
    4213
    >>> reverse_and_add(4213)
    7337
    """
    rev_n = from_digits(list(reversed(digits_of(n))))
    return n + rev_n
Пример #4
0
def check(digits):
    """
    >>> from utility import digits_of
    >>> check(digits_of(1406357289))
    True
    >>> check(digits_of(1406357298))
    False
    """
    digits = digits[1:] # ignore the first number
    for prime, number in zip(primes, tuplewise(3, digits)):
        number = from_digits(number)
        if not number % prime == 0:
            return False
    return True
Пример #5
0
def truncatable_primes():
    seen_primes = set()
    one_digit_primes = (2, 3, 5, 7)
    for p in primes():
        seen_primes.add(p)
        digits = digits_of(p)
        # One-digit primes are not considered truncatable.
        if len(digits) == 1:
            continue
        # All digits besides the first must be odd.
        if not all(d % 2 == 1 for d in digits[1:]):
            continue
        # First and last digit must be prime.
        if digits[0] not in one_digit_primes or \
           digits[-1] not in one_digit_primes:
            continue
        # Test whether each truncation is a prime.
        truncs = sorted(list(from_digits(d) for d in truncations(digits)))
        if all(t in seen_primes for t in truncs):
            yield p
Пример #6
0
import itertools
from utility import from_digits, tuplewise

primes = [2, 3, 5, 7, 11, 13, 17]
def check(digits):
    """
    >>> from utility import digits_of
    >>> check(digits_of(1406357289))
    True
    >>> check(digits_of(1406357298))
    False
    """
    digits = digits[1:] # ignore the first number
    for prime, number in zip(primes, tuplewise(3, digits)):
        number = from_digits(number)
        if not number % prime == 0:
            return False
    return True

solutions = []
for digits in itertools.permutations(range(10)):
    if check(digits):
        solutions.append(from_digits(digits))

print(sum(solutions))
Пример #7
0
def digit_permutations(n):
    for digits in itertools.permutations(digits_of(n)):
        yield from_digits(digits)