Example #1
0
# find the 12-digit number formed by concatenating a series of 3 4-digit
# numbers who are permutations of each other and are all prime

from itertools import permutations, dropwhile
from pe_utils import prime_sieve

prime_set = set(prime_sieve(10000))

def perm(n, inc):
    perm_set = set(map(lambda x: int("".join(x)), permutations(str(n))))
    perms = (n, n + inc, n + inc*2)
    if any(map(lambda x: x not in prime_set or x not in perm_set, perms)):
        return None
    else:
        return perms


primes = dropwhile(lambda x: x < 1000, prime_sieve(3333))
primes = filter(lambda x: x != None, map(lambda x: perm(x, 3330), primes))
primes = list(map(lambda x: x[0] * 10**8 + x[1] * 10**4 + x[2], primes))
print(primes)
Example #2
0
# Find the prime generating polynomial of form n^2 + an + b
# where |a| < 1000 & |b| < 1000 and generates the longest array of primes

from pe_utils import prime_sieve, discriminant_bc, complex_quad_polys, primes_to_n

primes = list(prime_sieve(10000))

# only quadratics with disriminant less than 0 can be prime generating
# |b| <= 63
# c cannot be negative, otherwise discriminant would be positive
# 1 <= c <= 1000
# |c| must be prime (1st iteration is always c, and since we're finding primes, c must always be prime)
poly_gen = complex_quad_polys(range(-63, 63 + 1), list(primes_to_n(1000)))


def polynomial(a, b, i):
    return i * i + a * i + b


highest_num_primes = 0
high_ab = None

for ab in poly_gen:
    iterations = 0
    while polynomial(ab[0], ab[1], iterations) in primes:
        iterations += 1

    if iterations > highest_num_primes:
        high_ab = ab
        highest_num_primes = iterations
        print(high_ab, highest_num_primes)
Example #3
0
# find the first four consecutive integers
# with unique prime factors

from pe_utils import prime_sieve, factor_map
from itertools import count, repeat, takewhile

distinct_factors = 4

prime_gen = prime_sieve()
primes = [next(prime_gen)]

factor_dict = {}

def n_distinct_factors(n):
    global factor_dict, primes, prime_gen
    if n in factor_dict:
        return factor_dict[n]
    else:
        if n in primes:
            return 1
        while primes[-1] < n:
            primes.append(next(prime_gen))
        distincts = len(list(factor_map(n, takewhile(lambda x: x < n, primes), True)))
        factor_dict[n] = distincts
        return distincts


for i in count(1):
    consec_nums = map(lambda x: x[0] + x[1], enumerate(repeat(i, distinct_factors)))
    consec_facs = map(lambda x: n_distinct_factors(x) == distinct_factors, consec_nums)
    if all(consec_facs):
Example #4
0
# find the greatest prime under 1000000 that is the sum of consecutive primes

from pe_utils import prime_sieve
from itertools import takewhile

max_sum = 1000000
primes = list(prime_sieve(max_sum))
num_primes = len(primes)

sums = list(takewhile(lambda x: x < max_sum, (sum(primes[:x]) for x in range(num_primes))))
num_sums = len(sums)

max_len = 0
def sum_and_bump(i, j):
    global max_len
    max_len = j - i
    return sums[j] - sums[i]

print(max([sum_and_bump(i, j) for i in range(num_sums) for j in range(i + 1 + max_len, num_sums) if j - i > max_len]))

# 997661
# 0.37 ms
# 2.40 GHz CPU
Example #5
0
# find the greatest prime under 1000000 that is the sum of consecutive primes

from pe_utils import prime_sieve
from itertools import takewhile

max_sum = 1000000
primes = list(prime_sieve(max_sum))
num_primes = len(primes)

sums = list(
    takewhile(lambda x: x < max_sum,
              (sum(primes[:x]) for x in range(num_primes))))
num_sums = len(sums)

max_len = 0


def sum_and_bump(i, j):
    global max_len
    max_len = j - i
    return sums[j] - sums[i]


print(
    max([
        sum_and_bump(i, j) for i in range(num_sums)
        for j in range(i + 1 + max_len, num_sums) if j - i > max_len
    ]))

# 997661
# 0.37 ms
Example #6
0
# Find the prime generating polynomial of form n^2 + an + b
# where |a| < 1000 & |b| < 1000 and generates the longest array of primes

from pe_utils import prime_sieve, discriminant_bc, complex_quad_polys, primes_to_n

primes = list(prime_sieve(10000))

# only quadratics with disriminant less than 0 can be prime generating
# |b| <= 63
# c cannot be negative, otherwise discriminant would be positive
# 1 <= c <= 1000
# |c| must be prime (1st iteration is always c, and since we're finding primes, c must always be prime)
poly_gen = complex_quad_polys(range(-63, 63 + 1), list(primes_to_n(1000)))

def polynomial(a, b, i):
	return i*i + a*i + b

highest_num_primes = 0
high_ab = None

for ab in poly_gen:
	iterations = 0
	while polynomial(ab[0], ab[1], iterations) in primes:
		iterations += 1

	if iterations > highest_num_primes:
		high_ab = ab
		highest_num_primes = iterations
		print(high_ab, highest_num_primes)

print(highest_num_primes, high_ab)
Example #7
0
# find the first four consecutive integers
# with unique prime factors

from pe_utils import prime_sieve, factor_map
from itertools import count, repeat, takewhile

distinct_factors = 4

prime_gen = prime_sieve()
primes = [next(prime_gen)]

factor_dict = {}


def n_distinct_factors(n):
    global factor_dict, primes, prime_gen
    if n in factor_dict:
        return factor_dict[n]
    else:
        if n in primes:
            return 1
        while primes[-1] < n:
            primes.append(next(prime_gen))
        distincts = len(
            list(factor_map(n, takewhile(lambda x: x < n, primes), True)))
        factor_dict[n] = distincts
        return distincts


for i in count(1):
    consec_nums = map(lambda x: x[0] + x[1],
Example #8
0
# Find the sum of all 11 primes that are truncatable from right to left and left to right

from pe_utils import prime_sieve
from math import log10
# from itertools import izip

truncatables = []

primes = set(list(prime_sieve(1000000)))

valid_least_sig = set([3, 7])
valid_most_sig = set([2, 3, 5, 7])
# valid_middle = set([1, 3, 7, 9])

def rtl_gen(n):
	for i in range(int(log10(n))):
		n /= 10
		yield n

def ltr_gen(n):
	for i in range(int(log10(n)), 0, -1):
		n %= 10**i
		yield n

for prime in primes:
	if prime > 7 and prime % 10 in valid_least_sig and prime / 10**(int(log10(prime))) in  valid_most_sig:
		truncatable = True
		for rtl in rtl_gen(prime):
			if rtl not in primes:
				truncatable = False
				break