def euler(): middle = math.ceil(math.sqrt(HIGHEST_N)) for p in range(middle, 0, -1): if prime.is_prime(p): for q in range(middle + 1, math.ceil((10 ** 7) / p)): if prime.is_prime(q): n = p * q t = discreet.totient(n) if sequence.is_permutation(str(n), str(t)): return n
def is_truncatable(number): if number < 10: return False for truncation in sequence.left_truncations(str(number)): if not primeutils.is_prime(int(truncation)): return False for truncation in sequence.right_truncations(str(number)): if not primeutils.is_prime(int(truncation)): return False return True
def euler(): # for each "starting" prime number for prime_number in prime.primes(200000): # list of integers for each digit prime_number_digits = list(int(digit) for digit in str(prime_number)) # set (without duplicates) of the digits in the prime number prime_number_digit_set = set(prime_number_digits) # for each digit that could be replaced in the prime number for base_digit in prime_number_digit_set: # number of digit replacements that are actual prime numbers prime_count = 0 # never replace the first digit with a zero replacements = range(10) if prime_number_digits[0] != base_digit \ else range(1, 10) # for each possible digit replacement for replacement_digit in replacements: # replace the digit base_digit with replacement_digit modified_digits = replace(prime_number_digits, base_digit, replacement_digit) # convert that list to a number modified_number = int(''.join(str(digit) \ for digit in modified_digits)) # if it's a prime, increment the prime count (duh) if prime.is_prime(modified_number): prime_count += 1 # return if the answer if we found it if prime_count == FAMILY_SIZE: return prime_number
def is_circular_prime(n, num_digs): cp = num_digs while cp > 0: n = ((n % 10) * 10**(num_digs - 1)) + n / 10 if not is_prime(n): return False cp -= 1 return True
def check_prime_permutations(i): if is_prime(i): nums = get_pandigital_nums(str(i)) pos = set() for n in nums: if is_prime(int(n)): pos.add(int(n)) nums.remove(n) if len(pos) >= 3: for a in pos: for b in pos: if a < b and a + ((b - a) * 2) in pos: return str(a) + str(b) + str(a + (b - a) * 2) else: return '' return ''
def euler(): # highest number of consecutive primes that add up to a prime highest_chain_length = 1 # highest resulting sum that was found highest_chain = 2 # for each prime for prime_number in prime.primes(MAX): # exit when it can't possibly be the start of a new chain if prime_number > MAX // highest_chain_length: break # start a chain from this prime number accumulator = prime_number # length of the current chain chain_length = 1 # for each prime starting from here for chain_prime in prime.primes(MAX): # exit when outside the bounds if accumulator > MAX: break # skip the primes below the "prime_number" start if chain_prime > prime_number: # if it's a prime and the chain length is longer than the # longest chain so far if prime.is_prime(accumulator) and \ chain_length > highest_chain_length: highest_chain_length = chain_length highest_chain = accumulator # increase chain length and add the next prime chain_length += 1 accumulator += chain_prime return highest_chain
def sequence_length(a, b): def f(): return n ** 2 + a * n + b n = 0 while f() > 1 and prime.is_prime(f()): n += 1 return n
def is_circular(prime): if prime in circular_cache: return circular_cache [prime] for rotation in sequence.rotations(str(prime)): if not primeutils.is_prime(int(''.join(rotation))): return False for rotation in sequence.rotations(str(prime)): circular_cache [int(''.join(rotation))] = True return True
def check_prime_sum(b, e, pos): global max_r global ans if pos < 1000000 and is_prime(pos): if e - b > max_r: max_r = e - b ans = pos elif e - b > max_r: check_prime_sum(b + 1, e, pos - primes[b]) check_prime_sum(b, e - 1, pos - primes[e])
def euler(): side_length = 2 accumulator = 1 prime_count = 0 number_count = 1 while side_length <= 8 or float(prime_count) / number_count >= 0.1: for i in range(4): accumulator += side_length number_count += 1 if prime.is_prime(accumulator): prime_count += 1 side_length += 2 return (side_length - 1)
def prime_sets(): """Yield all sets of possible answers for problem #49. Will 'yield' three arguments: the first, second and third prime. These three prime numbers are separated by exactly LEAP, and are permutations of each other. The three primes have exactly 4 digits.""" # for each prime number for first_prime in prime.primes(10 ** DIGITS // 3): # ensure that it has enough digits if first_prime > 10 ** (DIGITS - 1): # calculate the other two primes second_prime = first_prime + LEAP third_prime = first_prime + 2 * LEAP # invalid if they're not permutations of eachother if not is_int_permutation(first_prime, second_prime) or \ not is_int_permutation(first_prime, third_prime): continue # invalid if they're not actually primes if not prime.is_prime(second_prime) or \ not prime.is_prime(third_prime): continue # if this is reached, the three primes make up a possible answer yield first_prime, second_prime, third_prime
def euler(): # highest pandigital prime found so far highest_pandigital = 0 # for number of digits from 4 to 9 for length in range(4, 9): # generate the list of digits from 1 to n digits = list(range(1, length + 1)) # for each permutation of these digits for permutation in sequence.permutations(digits): number = int(''.join(str(digit) for digit in permutation)) # check if the number is prime if prime.is_prime(number): # set the new value of the highest pandigital prime if needed highest_pandigital = max(number, highest_pandigital) # return the highest pandigital prime found return highest_pandigital
#!/usr/bin/env python # # Jack Ferguson 2018 # # Problem # # q: Find the largest n-digit pandigital prime # a: 7652413 # from helpers.prime import is_prime from helpers.pandigital import get_pandigital_nums nums = get_pandigital_nums( '1234567') # there are no primes that are 9,8-dig pandigital ans = 0 for num in nums: if int(num) > ans and is_prime(int(num)): ans = int(num) print "p41: " + str(ans)
# # Jack Ferguson 2018 # # Problem 58 # # q: # a: # # this one needs a little work, right idea but there is a problem with the implementation from __future__ import division from helpers.prime import is_prime side = 3 diags = 2 prime_diags = 1 num = 5 i = 2 inc = 2 while prime_diags / diags >= .10: diags += 1 if is_prime(num): prime_diags += 1 num += inc i += 1 if i == 4: i = 0 inc += 2 side += 2 print 'p58: ' + str(side - 2)
# # Jack Ferguson 2018 # # Problem 50 # # q: # a: # from helpers.prime import is_prime primes = [] pos = 0 for i in range(2, 100000): if pos > 1000000: break if is_prime(i): pos += i primes.append(i) ans = 0 max_r = 0 def check_prime_sum(b, e, pos): global max_r global ans if pos < 1000000 and is_prime(pos): if e - b > max_r: max_r = e - b ans = pos elif e - b > max_r: check_prime_sum(b + 1, e, pos - primes[b])
#!/usr/bin/env python # # Jack Ferguson 2017 # # Problem 7 # # q: Find the 10001st prime # a: 104743 # from helpers.prime import is_prime NUM = 10001 num_prime = 0 ans = 0 while num_prime != NUM: ans += 1 if is_prime(ans): num_prime += 1 print "p7: " + str(ans)