コード例 #1
0
def is_circular_prime(n):
    """
    Return true if all rotations of digits of n are prime
    """
    # Single-digit numbers: easy
    if n < 10:
        return is_prime(n)

    # Multi-digit numbers: stick in a deque for easy rotation
    digits = deque()
    while n > 0:
        # If a digit is even, or 5, or 0, at least one rotation won't be prime
        digit = n % 10
        if digit == 0 or digit == 5 or digit % 2 == 0:
            return False

        digits.appendleft(digit)
        n //= 10

    for _ in range(len(digits)):
        candidate = 0
        multiplier = 1
        for d in digits:
            candidate += d * multiplier
            multiplier *= 10

        if not is_prime(candidate):
            return False

        digits.rotate()

    return True
コード例 #2
0
def is_prime_when_concatenated(first_prime,
                               second_prime,
                               prime_cache=None,
                               prime_cache_max=None):
    """
    Is first_prime prepended to second_prime a prime, and vice versa?
    """
    a = concatenate_digits(first_prime, second_prime)
    if not is_prime(a, prime_cache, prime_cache_max):
        return False

    b = concatenate_digits(second_prime, first_prime)
    return is_prime(b, prime_cache, prime_cache_max)
コード例 #3
0
def main():
    """
    Entry point
    """
    i = 33
    primes = [2]
    while True:
        # We're only interested in odd composite numbers
        if is_prime(i):
            i += 2
            continue

        # Can we compose this from a prime and twice a square?
        # Make sure we have enough primes
        while primes[-1] < i:
            primes.append(next_prime(primes[-1]))

        # Check primes, but not 2 as it's even and then the rest won't work out
        found_one = False
        for p in primes[1:-1]:
            # Is the remainder twice a square?
            half_remainder = (i - p) // 2
            root = math.sqrt(half_remainder)
            if int(root)**2 == half_remainder:
                # This one works...
                found_one = True
                break

        if not found_one:
            break

        i += 2

    print(f"This doesn't work: {i}")
コード例 #4
0
def is_right_truncatable_prime(p):
    """
    Is the prime p still prime for all right truncations?
    """
    truncated = p // 10
    while truncated > 0:
        if not is_prime(truncated):
            return False

        truncated //= 10

    return True
コード例 #5
0
def main():
    """
    Entry point
    """
    truncatable = []
    i = 10
    while len(truncatable) < 11:
        i += 1
        if is_prime(i) and is_left_truncatable_prime(
                i) and is_right_truncatable_prime(i):
            truncatable.append(i)

    print(f"Sum: {sum(truncatable)}")
コード例 #6
0
def is_left_truncatable_prime(p):
    """
    Is the prime p still prime for all left truncations?
    """
    truncate_modulo = 10
    truncated = p % truncate_modulo
    while truncated != p:
        if not is_prime(truncated):
            return False

        truncate_modulo *= 10
        truncated = p % truncate_modulo

    return True
コード例 #7
0
def main():
    """
    Entry point
    """
    num_prime = 3
    num_on_diagonals = 1 + 4
    size = 3
    while num_prime / num_on_diagonals > 0.1:
        size += 2
        bottom_right = size * size
        other_diags = [bottom_right - i * (size - 1) for i in range(1, 4)]
        num_prime += sum([1 for i in other_diags if is_prime(i)])
        num_on_diagonals += 4

    print(f"Side length with <10% prime along diag: {size}")
コード例 #8
0
def main():
    """
    Entry point
    """
    candidates = []
    for num_digits in range(4, 10):
        for perm in itertools.permutations(range(1, num_digits + 1),
                                           num_digits):
            n = 0
            for index, digit in enumerate(perm):
                n += digit * (10**index)

            if is_prime(n):
                candidates.append(n)
                print(n)

    print(f"Highest: {max(candidates)}")
コード例 #9
0
def main():
    """
    Entry point
    """
    # We consider quadratics of the form n^2 + an + b
    best = (0, )
    for a in range(-999, 1000):
        for b in range(-1000, 1001):
            n = 0
            while True:
                x = n * n + a * n + b
                if not is_prime(x):
                    break

                n += 1

            if n > best[0]:
                best = (n, a, b)

    print(f"Longest sequence ({best[0]} steps) for a={best[1]}, b={best[2]}")
    print(f"Coeff product: {best[1]*best[2]}")