def is_trunctable_prime(n): if not is_prime(n): return False n = str(n) for i in range(1, len(n)): if not is_prime(int(n[i:])) or not is_prime(int(n[:-i])): return False return True
def prime_factors(n): if is_prime(n): return [n] factors = [] for num in range(2, n // 2 + 1): if is_prime(num): if n % num == 0: factors.append(num) while n % num == 0: n //= num if n == 1: break continue return factors
# # There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. # # How many circular primes are there below one million? from Problem_26 import is_prime def rotations(n): digits = [int(d) for d in str(n)] permutations = [] while digits not in permutations: permutations.append(digits.copy()) digits.insert(0, digits.pop()) return permutations if __name__ == '__main__': circular_primes = [] for n in range(2, 1000000): if is_prime(n): rots = [is_prime(int("".join([str(d) for d in rotation]))) for rotation in rotations(n)] if False not in rots: temp_list = [] for rotation in rotations(n): temp_list.append(int("".join([str(d) for d in rotation]))) print(n, temp_list, rots) circular_primes.append(n) print(circular_primes) print("There are *{}* circular primes below one million.".format(len(circular_primes)))
# Pandigital prime # # We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. # For example, 2143 is a 4-digit pandigital and is also prime. # # What is the largest n-digit pandigital prime that exists? from Problem_26 import is_prime from Problem_32 import is_pandigital largest_n_digit_pandigital_prime = 1 # start from 7654321 because {987654321} (9-digit pandigitals) and {87654321} are divisible by 3 and so not prime # remember that if the sum of a number's digits is divisible by 3, the number is also divisible by 3 n = 7654321 while largest_n_digit_pandigital_prime == 1: if is_prime(n) and is_pandigital(n, n_max=len(str(n))): print(n, "is prime and is a {}-digit pandigital".format(len(str(n)))) largest_n_digit_pandigital_prime = n break n -= 2 print(largest_n_digit_pandigital_prime)
from Problem_26 import is_prime max_primes_generated = 0 max_a = 0 max_b = 0 for a in range(-999, 999, 2): for b in range(-1000, 1000): if not is_prime(abs(b)): continue primes_generated = 0 n = 0 while is_prime(n**2 + a * n + b): primes_generated += 1 n += 1 if primes_generated > max_primes_generated: max_primes_generated = primes_generated max_a = a max_b = b if primes_generated > 10: print("n**2 + " + str(a) + "n + " + str(b)) print(primes_generated) print(max_primes_generated, max_a, max_b, max_a * max_b)
# Prime permutations # # The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: # (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. # # There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, # but there is one other 4-digit increasing sequence. # # What 12-digit number do you form by concatenating the three terms in this sequence? from Problem_26 import is_prime primes = {} for n in range(1234, 9877): if is_prime(n): digits = ''.join(sorted(str(n))) if digits in primes.keys(): primes[digits].append(n) else: primes[digits] = [n] print(len(primes)) primes = dict(filter(lambda x: len(x[1]) > 2, primes.items())) print(len(primes), primes) for permutations in primes.values(): for p1 in range(len(permutations) - 2): if permutations[p1] + 3330 in permutations and permutations[ p1] + 6660 in permutations: print(str(permutations[p1]), str(permutations[p1] + 3330), str(permutations[p1] + 6660))