Esempio n. 1
0
def count_circular_primes(ceiling):
    """
    Counts the number of circular primes below ceiling.
    A circular prime is a prime for which all rotations of the digits is also
    prime.
    """
    return len([
        a for a in range(ceiling)
        if is_prime(a) and all(is_prime(a) for a in gen_rotation_list(a))
    ])
Esempio n. 2
0
def truncatable(num):
    """
    Returns true if num is a truncatable prime from left to right and right to
    left.
    A truncatable prime is a prime which remains prime as digits are removed
    from either end.
    """
    if len(str(num)) < 2 or not is_prime(num):
        return False
    return (all(is_prime(int(str(num)[:b])) for b in range(1, len(str(num))))
            and all(
                is_prime(int(str(num)[b:])) for b in range(1, len(str(num)))))
Esempio n. 3
0
def spiral_ratio(threshold):
    """
    Returns the side length of the square spiral for which the ratio of primes
    along both diagonals first falls below threshold.
    """
    prime_counter = 0
    for sp in count(2):
        prime_counter += sum(is_prime(a) for a in square_spiral_corners(sp))
        if (prime_counter / (4 * sp - 3)) < threshold:
            return 2 * sp - 1
Esempio n. 4
0
def largest_pandigital_prime():
    """
    Returns the largest n-digital pandigital prime.
    """
    largest = 0
    for setlen in range(9, 0, -1):
        if largest > 0:
            break
        for c in generate(1, setlen):
            if c > largest and is_prime(c):
                largest = c
    return largest
Esempio n. 5
0
def quadratic_primes(bound):
    """
    Returns the product of the coefficients a, b such that n^2 + an + b
    produces the most consecutive primes for values of n starting at 0 where
    abs(a) < bound and abs(b) <= bound.
    """
    product_primes = [1, 0]
    for a in range(-1 * bound + 1, bound):
        for b in range(-1 * bound, bound + 1):
            for n in count(0):
                if not is_prime(n**2 + a * n + b):
                    break
            if n > product_primes[1]:
                product_primes = [a * b, n]
    return product_primes[0]
Esempio n. 6
0
def goldbach(vol=0):
    """
    Finds the smallest odd composite that is not the sum of a prime and twice
    a square.
    """
    pset = set([2, 3, 5, 7])
    for cand in count(9, 2):
        if is_prime(cand):
            pset.add(cand)
            continue
        cset = set(cand - 2 * x * x for x in range(1, int(sqrt(cand / 2)) + 1))
        if cset & pset == set():
            if vol >= 1:
                print(f"{cand} is a counterexample.")
            return cand
Esempio n. 7
0
def smallestprimemember(family, searchlen):
    """
    Returns the smallest prime of the family if the family has size at least
    searchlen. Otherwise returns 0.
    """
    primes = []
    for d in range(10):
        if d == 0 and family[0] == "*":
            # Cannot replace leading star with zero
            continue
        if 10 - d < searchlen - len(primes):
            # Not enough left to satisfy searchlen
            return 0
        num = replacestar(family, d)
        if (is_prime(num)):
            primes.append(num)
    if len(primes) >= searchlen:
        return primes[0]
    else:
        return 0
Esempio n. 8
0
def pairwise(a, b):
    """
    Determines if the concatenation of a and b in both ways is prime
    """
    return is_prime(int(str(b) + str(a))) and is_prime(int(str(a) + str(b)))