def p060(): familys = [] for p in tqdm(primes()): for family in familys: candidate = [*family, p] if all( is_prime(int(f'{p}{f}')) and is_prime(int(f'{f}{p}')) for f in family): if len(candidate) >= 4: print(f'length:{len(candidate)}, sum:{sum(candidate):20}', candidate) if len(candidate) >= 5: return familys.append(candidate) familys.append([p])
def prime_family(prime, digit): members = 0 for dig in "0123456789": next = int(prime.replace(digit, dig)) if is_prime(next) and next > 100000: members += 1 return members == 8
def is_trunctable(n): if not is_prime(n): return False else: temp = n // 10 temp2 = n % 10 dividend = 10 while temp != 0: if not is_prime(temp): return False temp = temp // 10 while dividend < n: if not is_prime(temp2): return False dividend *= 10 temp2 = n % dividend return True
def answer(): pandigitals = perms(7654321) print(pandigitals) final = 0 for next in pandigitals: if is_prime(next): final = next break return final
def p058(): total = 1 primes = 0 length = 1 ds = diagonals() while primes / total >= 0.1 or primes == 0: length += 2 for _ in range(4): total += 1 if is_prime(next(ds)): primes += 1 print(length)
def answer(): prime = 0 diagonals = [1] ratio = 0.62 side = 3 while True: square = side**2 next1 = square - (side-1) next2 = square - (side-1)*2 next3 = square - (side-1)*3 if is_prime(next1): prime += 1 if is_prime(next2): prime += 1 if is_prime(next3): prime += 1 diagonals.extend([square, next1, next2, next3]) ratio = prime / len(diagonals) if ratio < 0.1: break side += 2 return side
def answer(): # Lower bound is 1487 found = 0 primes = primes_to(10000, 1487) for a in range(len(primes)): for b in range(a+1, len(primes)): prime_a = primes[a] prime_b = primes[b] c = prime_b + (prime_b - prime_a) if c < 10000 and is_prime(c): if is_perm(prime_a, prime_b) and is_perm(prime_a, c): found = str(prime_a) + str(prime_b) + str(c) break if found: break return found
def answer(): smallest = 1 proven = False primes = [2] while not proven: smallest += 2 if is_prime(smallest): primes.append(smallest) continue proven = True for prime in primes: if is_twiceSquare(smallest - prime): proven = False break return smallest
def answer(n): length = 0 longest = 0 primes = primes_to(n) # Make a list of sums prime_sum = [0] for x in range(len(primes)): prime_sum.append(prime_sum[x] + primes[x]) for a in range(len(prime_sum)): b = a - (length + 1) while b >= 0: dif = prime_sum[a] - prime_sum[b] if dif > n: b -=1 break if is_prime(dif): length = a - b longest = dif b -=1 print("The prime is: " + str(longest) + " and consists of " + str(length) + " primes.")
def test_p060(): assert all(is_prime(p) for p in concatenate_numbers([3, 7, 109, 673])) assert concatenating_primes([3, 7, 109, 673])
def concatenating_primes(numbers): return all(is_prime(p) for p in concatenate_numbers(numbers))
def right_truncatable_prime(p): return all(is_prime(n) for n in right_truncated(p))
def left_truncatable_prime(p): return all(is_prime(n) for n in left_truncated(p))
from euler_helpers import is_prime def circulate(n): """Return the list of numbers that are formed by cycling the digits of n""" digits = list(d for d in str(n)) for cycle in range(1, len(digits) + 1): yield int("".join(digits)) digits = digits[1:] + [digits[0]] checked = {} num = 0 for number in range(1, 1000001): if number in checked: continue circle = list(circulate(number)) if all(is_prime(c) for c in circle): num += len(circle) for c in circle: checked[c] = True checked = sorted(checked) print(checked) print(len(checked))
def first_non_goldbach(): for composite in tqdm(range(33, 10**5, 2)): if is_prime(composite): continue if not is_goldbach(composite): return composite
def is_prime_set(primes): return all(is_prime(int(str(prime[0]) + str(prime[1]))) for prime in itertools.permutations(primes, 2))