def p49(): primes_list = primes2.PrimeList() primes_list.sieve(10000) # Group together all primes in the same permutation group permutations = {} for prime in primes_list.get_primes_in(range(1000, 10000)): prime_sorted_digits = tuple(sorted(str(prime))) if prime_sorted_digits in permutations: permutations[prime_sorted_digits].append(prime) else: permutations[prime_sorted_digits] = [prime] # Keep only those with at least three primes permutations = { sorted_digits: primes for sorted_digits, primes in permutations.items() if len(primes) >= 3 } # Get arithmetic sequences of length 3 progressives = [] for primes in permutations.values(): # Pick all prime triplets for combo in combinations(primes, 3): #Check for progression if combo[2] - combo[1] == combo[1] - combo[0]: progressives.append(combo) #There should be two sequences assert len(progressives) == 2 # Remove known sequence progressives.remove((1487, 4817, 8147)) the_seq = progressives[0] return ''.join(str(prime) for prime in the_seq)
def p41(): MAX_NUM = 7654321 prime_list = primes2.PrimeList() prime_list.sieve(MAX_NUM) max_prime_pandigital = 0 for prime in prime_list.get_primes_in(generic.grange(MAX_NUM, -1)): if ''.join(sorted(str(prime))) == '1234567': max_prime_pandigital = prime break return max_prime_pandigital
def p46(): primes = primes2.PrimeList() for odd in generic.grange(35, 2): if primes.is_prime(odd): continue for psquare in range(1, math.floor(math.sqrt((odd - 3) / 2)) + 1): square = 2 * psquare**2 if primes.is_prime(odd - square): break else: return odd
def p35(): prime_list = primes.PrimeList() prime_list.sieve(1000000) number = 1 #NOTE: 2 is blocked out by (***) for prime in prime_list.get_primes(): if prime >= 10**6: break #(***) if any(x in str(prime) for x in ["0", "2" ,"4", "6", "8"]): continue for rotation in integers.get_rotations(prime): if not prime_list.is_prime(rotation): break else: #Did not break => They are all prime number += 1 return number
Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. Answer: 748317 """ """Thoughts: Note 379 is not a truncatable prime even though it is from 3797. """ import utility.primes2 as p33 pl = p33.PrimeList() def get_truncates(prime): #Remove from right str_prime = str(prime) if not pl.is_prime(int(str_prime[-1]))\ or not pl.is_prime(int(str_prime[0])): return None truncated_primes = [] #Truncate from right first for i in range(1, len(str_prime)-1): new_prime = int(str_prime[:-i]) if not pl.is_prime(new_prime): break else:
def setUp(self): self.prime_list = primes.PrimeList()