def right_trunc(p): if len(str(p)) > 1: p //= 10 if is_prime(p): return right_trunc(p) else: return False else: if is_prime(p) and p >= 2: return True
def left_trunc(p): tmp_str = str(p) if len(tmp_str) > 1: p = int(tmp_str[1:]) if is_prime(p): return left_trunc(p) else: return False else: if is_prime(p) and p >= 2: return True
def is_circular_prime(x): s = str(x) for i in range(len(s)): rotation = s[i:] + s[:i] if not is_prime(int(rotation)): return False return True
def permutations(val, perm_set, step = 0): if step < len(val): for i in range(step, len(val)): # copy the string (store as array to swap indices) perm_string = [c for c in val] # swap the current index with the step perm_string[step], perm_string[i] =perm_string[i], perm_string[step] #rejoin into integer term = int("".join(perm_string)) if is_prime(term): perm_set.add(term) # recurse on the portion of the string that has not been swapped yet permutations(perm_string, perm_set, step + 1) return perm_set
def prime_trunc(n): if has_blocked_digits(n): return False elif is_prime(n): if right_trunc(n): return left_trunc(n)
return perm_set def prime_perms(n): my_seq = set() perm_list = permutations(str(n), my_seq) perm_list.remove(n) for perm in perm_list: b = perm + (perm - n) if b in perm_list: return [n, perm, b] return [] #Driver code if __name__ == '__main__': start_time = time.time() done = False n = 999 seq = [] while not done: n += 1 if not has_blocked_digits(n) and is_prime(n): seq = prime_perms(n) if len(seq) == 3 and 1487 not in seq: done = True print(''.join(map(str, seq))) print("Runtime: {}".format(time.time() - start_time))
import time import math from problem27 import list_primes, is_prime def gold_num(n): prime_list = list_primes(n - 2) is_gold_num = False for k in range(1, int(math.sqrt(n / 2) + 1)): p = n - 2 * k**2 if p in prime_list: is_gold_num = True break return is_gold_num # driver code if __name__ == '__main__': start_time = time.time() done = False n = 33 while not done: n += 2 if not is_prime(n) and not gold_num(n): done = True print('Result: {}'.format(n)) print('Runtime: {}'.format(time.time() - start_time))