Пример #1
0
def answer(limit):
    result = []
    primes = set(sieve_of_eratosthenes(limit))
    for p in primes:
        if is_circular_prime(p, primes):
            result.append(p)

    print len(result)
Пример #2
0
def answer(limit):
	result = []
	primes = set(sieve_of_eratosthenes(limit))
	for p in primes:
		if is_truncatable_prime(p, primes):
			result.append(p)
	print result
	print sum(result) - 17
Пример #3
0
def answer():
    primes = set([x for x in sieve_of_eratosthenes(10 ** 4) if x >= 1000 and x <= 9999])

    for p in primes:
        p_str = str(p)
        if "".join(sorted(str(p))) == "1478":
            continue
        seq = set()
        perms = itertools.permutations(p_str)
        for perm in perms:
            v = int("".join(perm))
            if v in primes:
                seq.add(v)
        if len(seq) >= 3:
            equi = equidistant_elements(list(seq))
            if equi:
                return "".join([str(x) for x in equi])
Пример #4
0
def answer():
	primes = sieve_of_eratosthenes(10**6)
	prime_set = set(primes)
	n = 1
	while True:
		# these are only the odd numbers, but we take care of primes
		# using the sqrt check below
		n += 2
		found = False
		for i in xrange(len(primes)):
			p = primes[i]
			if p > n: 
				break
			diff = math.sqrt((n - p) / 2.0)
			if int(diff) == diff:
				found = True
		if not found:
			return n
Пример #5
0
def answer():
	primes = sieve_of_eratosthenes(10**6)
	prime_set = set(primes)
	max_consecutive = 0
	max_number = 0
	for i in xrange(len(primes)-1):
		val = primes[i]
		counter = 1
		for j in xrange(i+1, len(primes)):
			val += primes[j]
			if val > 10**6:
				break
			counter += 1
			if val in prime_set:
				if max_consecutive < counter:
					max_consecutive = counter
					max_number = val
	return max_number, max_consecutive
Пример #6
0
def answer():
	# Primes could be given in any other way; here, we just use a simple sieve.
	primes = set(sieve_of_eratosthenes(10**7))
	max_num_primes = 0
	product = 0
	for a in xrange(-1000, 1000):
		for b in xrange(-1000, 1000):
			num_primes = 0
			n = 0
			while True:
				if int(abs(n**2 + a*n + b)) in primes:
					num_primes += 1
					n += 1
				else:
					break
			if num_primes > max_num_primes:
				max_num_primes = num_primes
				product = a*b
	return product
Пример #7
0
def answer():
	primes = sieve_of_eratosthenes(10**6)
	factors1 = 0
	factors2 = 0
	factors3 = 0
	n = 1
	while True:
		factors = 0
		for p in primes:
			if p > n:
				break
			if n % p == 0:
				factors += 1
		if factors1 >= 4 and factors2 >= 4 and factors3 >= 4 and factors >= 4:
			return n-3
		factors1 = factors2
		factors2 = factors3
		factors3 = factors
		n += 1
Пример #8
0
import sys
from helpers import sieve_of_eratosthenes, memoize, is_prime

concat_primes = dict()
limit = 20000
primes = sieve_of_eratosthenes(limit)

def concat(a, b):
	return int(str(a) + str(b))

#Memoized to avoid duplicate computation
# Returns the set of prime indices which, when concatenated primes[i],
# will yield another prime
@memoize
def get_concat_primes(i):
	prime = primes[i]
	prime_concats = set()
	for j in xrange(i+1, len(primes)):
		prime_j = primes[j]
		c1 = concat(prime, prime_j)
		c2 = concat(prime_j, prime)
		c1_prime = c1 in primes if c1 < limit else is_prime(c1)
		c2_prime = c2 in primes if c2 < limit else is_prime(c2)
		if c1_prime and c2_prime:
			prime_concats.add(j)
	return prime_concats
		
def answer():
	min_sum = sys.maxint
	for i in xrange(len(primes)):
		prime_i = primes[i]
Пример #9
0
from helpers import sieve_of_eratosthenes, memoize
import itertools
import sys

primes = sieve_of_eratosthenes(10**6)
prime_set = set(primes)


@memoize
def check_val(p_str):
	num_primes = 0
	smallest_prime = sys.maxint
	start = 1 if p_str[0] == '*' else 0
	r = range(start, 10) if p_str[-1] != '*' else [1, 3, 7, 9]
	for i in r:
		p = int(p_str.replace('*', str(i)))
		if p in primes:
			num_primes += 1
			smallest_prime = min(smallest_prime, p)
	if num_primes >= 8:
		print "FOUND ", smallest_prime
		sys.exit(0)
	

def generate_wildcards(p):
	s = str(p)
	r = range(len(s))
	for i in xrange(1, len(s)):
		for c in itertools.combinations(r, i):
			l = list(s)
			for i in c: