def truncatable(n): # test for left truncatable n_str = str(n) for i in range(1, len(n_str)): if not is_prime(int(n_str[i:])): return False # test for right truncatable for i in range(1, len(n_str)): if not is_prime(int(n_str[:i])): return False return True
def build_grid(size): """ 1. build a "size" X "size" grid, and fill in the numbers 2. keep track of "diagonal_primes" as we are filling in the numbers 3. keep track of "diagonal_numbers" as we are filling in the numbers """ # grid = [[0] * size for i in range(size)] move = movegen() diagonal_primes = 0 diagonal_numbers = 0 i = j = size / 2 for n in range(1, size * size + 1): # grid[i][j] = n if in_diagonal(i, j, size): # interesting things happening in the diagonal diagonal_numbers += 1 if is_prime(n): diagonal_primes += 1 direction = next(move) if direction == "R": j += 1 elif direction == "D": i += 1 elif direction == "L": j -= 1 else: i -= 1 return diagonal_primes, diagonal_numbers
def is_the_one(first_term): first_term_str = str(first_term) second_term = first_term + 3330 second_term_str = str(second_term) third_term = second_term + 3330 third_term_str = str(third_term) if set(first_term_str) == set(second_term_str) == set(third_term_str) and is_prime(second_term) and is_prime(third_term): return first_term_str + second_term_str + third_term_str else: return False
# How many circular primes are there below one million? from time import time from UsefulFunctions import primes, is_prime, rotations start_time = time() p = primes() candidate = next(p) l = [] while candidate < 1000000: still_prime = True for e in rotations(candidate): if not is_prime(e): still_prime = False break if still_prime: l.append(candidate) candidate = next(p) print l print len(l) l = filter(lambda x: str(x).count("0") == 0, l) # 101, 103 etc. don't count print l print len(l) print "Total Time: ", time() - start_time
start_time = time() # Method 1 - takes too long, it's so stupid, needs to wait till beyond 987654321 # p = primes() # c = next(p) # answer = 0 # while c < 987654321: # if pandigital(c): # answer = c # c = next(p) p = pandigitals() while True: raw_n = next(p) if raw_n[-1] % 2 == 0 or raw_n[-1] == 5: # filter out the even & 5 ending continue # takes .5 seconds off n = reduce(lambda x, y: 10 * x + y, raw_n) if is_prime(n): print n break print "Total Time: ", time() - start_time # Completed on Tue, 4 Mar 2014, 23:58 # Solve by: 33179 # --------------- # 7652413 # Total Time: 0.734999895096 # [Finished in 0.9s]
while sum_ < 1000000: plist.append(p) ubterms += 1 sum_ += p p = next(pgen) print ubterms # calculating all the possible sums d = {} # going to store a prime number & it's associated number of terms for sliding_size in range(2, ubterms + 1): # calculate first window sum for new slice sum_ = 0 for i in range(sliding_size): sum_ += plist[i] if sum_ % 2 != 0 and is_prime(sum_): d[sum_] = sliding_size # calculate sum for new window for i in range(1, ubterms - sliding_size): sum_ += plist[i + sliding_size - 1] - plist[i - 1] if sum_ % 2 != 0 and is_prime(sum_): d[sum_] = sliding_size m = max(d.values()) for k in d: if d[k] == m: print "Total #: %d Prime #: %d has %d terms" % (len(d), k, d[k]) print "Total Time: ", time() - start_time
def ocgen(): n = 9 while True: if not is_prime(n): yield n n += 2
# starting with n = 0. from UsefulFunctions import is_prime from time import time def f(n, a, b): return n * (n + a) + b start_time = time() d = {} for a in range(-999, 1000): for b in range(-999, 1000): n = 0 while is_prime(f(n, a, b)): n += 1 d[(a, b)] = n m = max(d.values()) for a, b in d: if d[(a, b)] == m: print "a=%d, b=%d, a*b=%d -> %d" % (a, b, a * b, m) print "Total Time: ", time() - start_time # Completed on Tue, 4 Mar 2014, 04:52 # Solve by: 41748 # --------------- # a=-61, b=971, a*b=-59231 -> 71