def truncatable_primes(limit=None): """Returns all truncatable primes up to limit, or optionally all 11.""" # This is similar to the circular primes problem in that we can eliminate # a lot of primes that may have even numbers or 5s anywhere but the first # digit. primes = prime_sieve(limit) if limit else prime_sieve(1000000) digits = '024568' end_dg = '14689' primes = [x for x in primes if not any([d in str(x)[1:] for d in digits])] primes = [x for x in primes if not any([d in str(x)[0] for d in end_dg])] primes = [x for x in primes if not any([d in str(x)[-1] for d in end_dg])] primes = primes[4:] p_gen = prime_generator(1000000) trunc_primes = [] i = 0 while len(trunc_primes) < 11 and (i < len(primes) or not limit): try: p = str(primes[i]) except IndexError: p = str(next(p_gen)) for j in range(1, len(p)): if not is_prime(int(p[j:])) or not is_prime(int(p[:-j])): break else: trunc_primes.append(int(p)) i += 1 return trunc_primes
def all_concat_prime_with(primes, new_element): for prime in primes: c = int(str(prime) + str(new_element)) if c not in cache: cache[c] = is_prime(c, cached_primes) if not cache[c]: return False c = int(str(new_element) + str(prime)) if c not in cache: cache[c] = is_prime(c, cached_primes) if not cache[c]: return False return True
def is_circular_prime(i): circular_primes_list = [i] for rotation in generate_rotations(i): circular_primes_list.append(rotation) if not is_prime(rotation): return False return circular_primes_list
def all_concat_prime(p): for combination in itertools.permutations(p, 2): c = int(str(combination[0]) + str(combination[1])) if c not in cache: cache[c] = is_prime(c) if not cache[c]: return False return True
def is_truncatable_prime(prime): if prime < 10: return False digits = str(prime) while len(digits) > 1: digits = digits[1:] if not is_prime(int(digits)): return False digits = str(prime) while len(digits) > 1: digits = digits[:-1] if not is_prime(int(digits)): return False return True
def find_n(a, b, max_n): for n in xrange(0, max_n): val = formula(a, b, n) prime = is_prime(val) if not prime: return n - 1 return max_n
def prime_concats(n): """ Generates a dictionary of primes less than n with a list of other primes that, when concatenated together, also form a prime. """ primes = prime_sieve(n) prime_dict = {} for i in range(len(primes)): p = primes[i] prime_dict[p] = [] for q in primes[i + 1:]: if is_prime(int(str(p) + str(q))) and is_prime( int(str(q) + str(p))): prime_dict[p].append(q) prime_concat_list = {} for p in prime_dict: if prime_dict[p] != []: prime_concat_list[p] = prime_dict[p] return prime_concat_list
def prime_generator(n): """Generates primes greater than n.""" while True: if n % 6 == 1: n += 4 elif n % 6 == 5: n += 2 else: n += 1 if is_prime(n): yield n
def sum_primes(n): """ Sum up all prime numbers below n. """ sum = 0 for i in range(n): # edge cases are handled by `is_prime' if is_prime(i): sum += i return sum
def largest_pandigital_prime(): # The prime cannot have 9 digits, because the sum of the numbers 1 through # 9 is divisible by 9, and hence will never be prime. The same logic holds # for 8, as eliminating 9 from a number divisible by 9 is still divisible. pandigitals = pandigital_generator(7, True) while True: p = next(pandigitals) if str(p)[-1] in '024568': continue if is_prime(p): break return p
def prime_sum(limit): if limit < 2: raise ValueError("limit must be >= 2") primeSum = 2 primes = [2] i = 3 while True: if is_prime(i, primes): primeSum += i primes.append(i) i += 2 if i > limit: return primeSum
def diagonals(n): if n <= 0: return ([1], []) if n in cache: return cache[n] o, p = diagonals(n - 1) o = o[:] # copy cached list p = p[:] # copy cached list for i in range(4): u = o[-1] + n * 2 if is_prime(u): p.append(u) o.append(u) result = (o, p) cache[n] = result return result
def longest_quadratic_primes(limit): b_list = prime_sieve(limit) # Because f(0) = b, so b must be prime. # Further, when n = 1, p + 1 + even number will produce a non-prime. So a # must be odd. The only exception would be if b = 2, in which case if n = 2, # n² = 4 and a must again be odd to produce any more primes. a_list = [x for x in list(range(-limit + 1, limit)) if x % 2 == 1] max_length, max_a, max_b = 0, 0, 0 for a in a_list: for b in b_list: n = 0 while is_prime(n**2 + a * n + b): n += 1 if n > max_length: max_length = n max_a = a max_b = b return (max_a, max_b)
def nth_prime(n): """ Determine the n'th prime number using a brute-force approach. """ if n == 1: return 2 current_num = 3 num_primes = 1 # 2 is already included while True: if is_prime(current_num): num_primes += 1 if num_primes == n: return current_num current_num += 2 # even numbers aren't prime!
def solve(): i = 7.0 while True: i += 2.0 if is_prime(i): continue for prime in generate_primes(): if prime > i: return i r = i - prime if not r % 2 == 0: continue r = math.sqrt(r / 2.0) if r == math.floor(r): break pass
def solve(limit=1000, prec=50, debug=False): longest = 0 longest_i = 0 primes = [2] for i in xrange(2, limit + 1): if debug and i % 10 == 0: print ".", sys.stdout.flush() if is_prime(i, primes): num = precise_quotient(1, i, prec) length = longest_recurring_cycle(num.replace("0.", "")) if length > longest: longest = length longest_i = i if debug: print("") print("longest: 1/" + str(longest_i) + " with len = " + str(longest)) return longest
def test_is_prime(self): self.assertFalse(problem003.is_prime(1)) self.assertTrue(problem003.is_prime(2)) self.assertFalse(problem003.is_prime(4)) self.assertTrue(problem003.is_prime(7)) self.assertFalse(problem003.is_prime(20))