Ejemplo n.º 1
0
def p37():
    prime_numbers = list(primes(740000))
    truncatable_prime_list = []
    for prime in prime_numbers:
        if prime < 10: continue
        prime_str = str(prime)
        if prime_str[-1] not in ('3', '7'): continue
        if prime_str[0] not in ('2', '3', '5', '7'): continue

        right_truncatable_prime = False
        right_truncated_str = prime_str[:-1]
        while len(right_truncated_str) > 0:
            right_truncated = int(right_truncated_str)
            if is_prime(right_truncated, prime_numbers) == False: break
            right_truncated_str = right_truncated_str[:-1]
        else:
            right_truncatable_prime = True
        if right_truncatable_prime == False: continue

        left_truncatable_prime = False
        left_truncated_str = prime_str[1:]
        while len(left_truncated_str) > 0:
            left_trucated = int(left_truncated_str)
            if is_prime(left_trucated, prime_numbers) == False: break
            left_truncated_str = left_truncated_str[1:]
        else:
            left_truncatable_prime = True

        if left_truncatable_prime and right_truncatable_prime:
            truncatable_prime_list.append(prime)

    return sum(truncatable_prime_list)
Ejemplo n.º 2
0
def prepare_concat_map(source_primes: list[int],
                       check_primes: list[int]) -> dict[int, list[int]]:
    left_to_right = {}
    for prime_pair in combinations(source_primes, 2):
        left_prime = str(prime_pair[0])
        right_prime = str(prime_pair[1])
        left_right_prime = is_prime(int(left_prime + right_prime),
                                    check_primes)
        right_left_prime = is_prime(int(right_prime + left_prime),
                                    check_primes)
        if left_right_prime and right_left_prime:
            left_to_right.setdefault(prime_pair[0], []).append(prime_pair[1])
    return left_to_right
Ejemplo n.º 3
0
def p27(a_start: int, a_stop: int, b_start: int, b_stop: int) -> int:
    # b needs to be prime to get a prime when n == 0, so b >= 2
    b_start = max(b_start, 2)

    # when n == b then all terms are a multiple of b, so this is the
    # limit of what needs to be searched
    largest_output = (b_stop - 1) * ((b_stop - 1) + (a_stop - 1) + 1)
    prime_factors = list(primes(largest_output))

    # calcuate offset for start of a: a must be odd, unless b == 2
    a_start_even = a_start % 2 == 0

    (max_a, max_b, max_n) = (0, 0, 0)

    for b in prime_factors:
        if b < b_start: continue
        if b >= b_stop: break
        
        if a_start_even != (b == 2):
            a_start_offset = 1
        else:
            a_start_offset = 0 
        
        for a in range(a_start + a_start_offset, a_stop, 2):
            for n in range(0, b):
                quadratic_value = n * n + a * n + b
                if quadratic_value < 0: break
                if not is_prime(quadratic_value, prime_factors): break
            if n > max_n:
                (max_a, max_b, max_n) = (a, b, n)
    
    return max_a * max_b
Ejemplo n.º 4
0
def prime_plus_twice_square(num):
    i = 1
    while num > 2 * (i**2):
        candidate = num - 2 * (i**2)
        if is_prime(candidate):
            return [candidate, i]
        else:
            i += 1
    return []
Ejemplo n.º 5
0
def is_truncatable_prime(n):
    # Single digit primes cannot be truncatable.
    if n < 10:
        return False

    # Calculate left and right truncations.
    digits = digit_expansion(n)
    truncations = prefixes(digits) + suffixes(digits)
    truncations = map(digit_unexpansion, truncations)

    # And test truncations are primes.
    return all(is_prime(truncation) for truncation in truncations)
Ejemplo n.º 6
0
def p58(max_ratio: float) -> int:
    prime_list = list(primes(1000000))
    layer = 0
    ratio = 1
    prime_count = 0
    number_count = 1
    while ratio > max_ratio:
        layer += 1
        for corner_number in range(4):
            number = 2 * layer * (2 * (layer - 1) + corner_number + 1) + 1
            if is_prime(number, prime_list):
                prime_count += 1
        number_count += 4
        ratio = prime_count / number_count

    return 2 * layer + 1
Ejemplo n.º 7
0
def p35(stop: int) -> int:
    circular_primes = set()
    primes_to_check = list(primes(stop))
    for prime in primes_to_check:
        prime_str = str(prime)

        prime_len = len(prime_str)
        rotated_str = prime_str
        rotations = [rotated_str]
        prime_rotations = 1
        for rotation_number in range(prime_len - 1):
            rotated_str = rotate(rotated_str)
            rotations.append(rotated_str)
            if is_prime(int(rotated_str), primes_to_check):
                prime_rotations += 1
            else:
                break

        if prime_rotations == prime_len:
            # all rotations are prime
            circular_primes.update(rotations)

    return len(circular_primes)
Ejemplo n.º 8
0
def is_circular(n):
    '''
        Return True if n is a circular prime for n > 0.
    '''
    return all(is_prime(rotation) for rotation in rotations(n))
Ejemplo n.º 9
0
    bound = bound + 1

array = []

while True:
    bound = bound - 1 #narrow maximum width of search
    shift = 0 #reinitialize shift
    #NOTE: array should not need reinitialized because this code only executes if it's empty
    print("bound = ", bound)
    while True:
        candidate = 0
        #add up the primes between index j and bound + j to produce a candidate
        for k in range(shift, bound + shift):
            candidate = candidate + primes[k]

        if candidate <= max and is_prime(candidate): #if candidate meets selection criteria, record it
            array.append(candidate)
            shift = shift + 1
            print(candidate,' - candidate added')
        elif candidate <= max:
            shift = shift + 1
            print(candidate,' - candidate was not prime')
        else:
            print(candidate,' - candidate overstepped bounds')
            break
    print('while loop finished', "\n\n")

    #detects if the inner loop found any candidates
    if len(array) > 0:
        array.sort()
        largest = array[len(array)-1] #picks out largest element which is prime
Ejemplo n.º 10
0
candidates = []
final = []

for i in primes:
    #takes the current element of primes and outputs a list of all its permutations
    current_array = num_to_array(i)
    curr_perms_arr = permutations(current_array)
    curr_perms = []
    for j in range(0,len(curr_perms_arr)):
        curr_perms.append(array_to_num(curr_perms_arr[j]))

    #determines which if any permutations are prime
    prime_perms = []
    for j in range(0,len(curr_perms)):
        if is_prime(curr_perms[j]) and curr_perms[j] > 1000:
            prime_perms.append(curr_perms[j])

    #formats list
    prime_perms = remove_duplicates(prime_perms)
    prime_perms.sort()

    if(len(prime_perms)) >= 3:
        candidates.append(prime_perms)

candidates = remove_duplicates(candidates)

#find differences among elements of candidates and checks if that difference contains 3330
for i in range(0,len(candidates)):
    diff = []
    for j in range(0,len(candidates[i])):