Exemplo n.º 1
0
def valid_pair_set(x, N=2 * 10**4):
    primes = set([i for i in erathos(N) if i > x])
    primes -= {x}
    valids = []
    x = str(x)
    for p in primes:
        if is_prime(int(str(p) + x)) and is_prime(int(x + str(p))):
            valids.append(p)
    return valids
Exemplo n.º 2
0
def is_truncatable(n):
	if not e.is_prime(n):
		return False
	
	s = str(n)
	for d in xrange(1,len(s)):
		r = int(s[d:])
		l = int(s[:d])
		if not e.is_prime(l) or not e.is_prime(r):
			return False
	return True
Exemplo n.º 3
0
def goldbach2():
    n = 35
    while True:
        if not is_prime(n) and n % 2 == 1:
            y = 1
            found = False
            while y <= ceil(sqrt((n-1)/2)) and not found:
                x = n - 2 * y**2
                if is_prime(x):
                    found = True
                y += 1
            if not found:
                return n
        n += 1
Exemplo n.º 4
0
def yield_primes():
    yield 2
    result = 3
    while True:
        if is_prime(result):
            yield result
        result += 2
Exemplo n.º 5
0
def max_pandigital_prime():
    for i in range(9, 0, -1):
        digits = [str(x) for x in range(1, i+1)]
        perms = list(permutations(digits))
        ints = [int(''.join(p)) for p in perms]
        for x in sorted(ints, reverse=True):
            if is_prime(x):
                return x
Exemplo n.º 6
0
def is_truncatable_prime(x, dir=0):
    d = list(str(x))
    truncs = []
    if dir == 0:
        truncs = [d[i:] for i in range(len(d))]
    else:
        truncs = [d[:i] for i in range(1, len(d) + 1)]
    truncs = [int(''.join(t)) for t in truncs]
    return False not in [is_prime(t) for t in truncs]
Exemplo n.º 7
0
def problem_41():
    for length in xrange(9, 1, -1):
        options = utils.pandigitals(length)
        for num in options:
            if utils.is_prime(num):
                return num
Exemplo n.º 8
0
'''
Spiral primes
----
Starting with 1 and spiralling anticlockwise in the following way, a square
spiral with side length 7 is formed. It is interesting to note that the odd
squares lie along the bottom right diagonal, but what is more interesting is
that 8 out of the 13 numbers lying along both diagonals are prime; that is, a
ratio of 8/13 ≈ 62%. If one complete new layer is wrapped around the spiral
above, a square spiral with side length 9 will be formed. If this process is
continued, what is the side length of the square spiral for which the ratio of
primes along both diagonals first falls below 10%?

Note: the values of the diagonal elements are specified in problem 28, they are
just reversed due to the anticlockwise spiral formation. The bottom right
element need never be tested for primality because it's a square.
The execution time is quite long, it can be shortened by adopting a more
efficient primality test.
'''

from euler_utils import is_prime

if __name__ == '__main__':
    n_primes, k = 3, 1
    while n_primes / (4*k+1) > 0.1:
        k += 1
        prev_max = (k*2-1)**2
        diag_elements = (prev_max+2*k, prev_max+4*k, prev_max+6*k)
        n_primes += sum([1 for i in diag_elements if is_prime(i)])
    print("Spiral side length: " + str(2*k+1))
Exemplo n.º 9
0
def problem_41():
    for length in xrange(9, 1, -1):
        options = utils.pandigitals(length)
        for num in options:
            if utils.is_prime(num):
                return num