Esempio n. 1
0
def is_circular(n):
    if is_prime(n):
        r = rotate(n)
        while (r != n):
            if not is_prime(r):
                return False
            r = rotate(r)
        return True
    else:
        return False
Esempio n. 2
0
def find_nth_prime(n):
    cur = 2  # init: 第一个prime是2
    cnt = 1  # init

    while cnt <= n:
        if is_prime(cur) and cnt == n:  # 退出条件先写
            return cur
        if is_prime(cur):
            cnt += 1
        cur += 1

    return -1
Esempio n. 3
0
def is_trunc(p):
	single_primes = [2,3,5,7]
	
	if not ((p % 10) in single_primes): return False
	if not ((p / 10 ** (len(str(p))-1)) in single_primes): return False
	
	left = p / 10
	while left > 0:
		if not euler_util.is_prime(left): return False
		left /= 10
		
	right = p % (10 ** (len(str(p))-1))
	while right > 0:
		if not euler_util.is_prime(right): return False
		right = right % (10 ** (len(str(right))-1))
		
	return True
Esempio n. 4
0
def is_trunc(p):
    single_primes = [2, 3, 5, 7]

    if not ((p % 10) in single_primes): return False
    if not ((p / 10**(len(str(p)) - 1)) in single_primes): return False

    left = p / 10
    while left > 0:
        if not euler_util.is_prime(left): return False
        left /= 10

    right = p % (10**(len(str(p)) - 1))
    while right > 0:
        if not euler_util.is_prime(right): return False
        right = right % (10**(len(str(right)) - 1))

    return True
Esempio n. 5
0
def find_prime_under_ceiling_sum(ceiling):
    cur = 0
    res = 0
    while cur < ceiling:
        if is_prime(cur):
            res += cur
        cur += 1
    return res
Esempio n. 6
0
def is_truncatable(n, leftToRight):
	if is_prime(n):
		if len(str(n)) == 1:
			return True
		if leftToRight:
			return is_truncatable(int(str(n)[1:]), True)
		else:
			return is_truncatable(int(str(n)[:-1]), False)
	else:
		return False
Esempio n. 7
0
def euler41():
    """http://projecteuler.net/index.php?section=problems&id=41

    What is the largest n-digit pandigital prime?"""
    primes = []
    # note: sum(1..8) % 3 == sum(1..9) % 3 == 0, so 7 digits is the max
    for perm in permutations("1234567"):
        val = int(''.join(perm))
        if is_prime(val):
            primes.append(val)
    return max(primes)
Esempio n. 8
0
def euler58():
    """http://projecteuler.net/problem=58

    Investigate the number of primes that lie on the diagonals of the spiral
    grid."""
    value, side, count = 1, 2, 0
    primes, total = 0, 0
    while True:
        primes, total = primes + is_prime(value), total + 1
        if count == 0:
            if primes > 0 and float(primes)/total < 0.1:
                return side - 1
        count, value = count + 1, value + side
        if count == 4:
            side, count = side + 2, 0
Esempio n. 9
0
def euler41():
	"""
	We shall say that an n-digit number is pandigital if it makes use of all the
	digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is
	also prime.
	
	What is the largest n-digit pandigital prime that exists?
	"""
	# last digit can't be 5 or even
	last_digits = [1,3,7,9]
		
	# all pandigitals of length 8 and 9 are divisible by 3 since the sum of their
	# digits (36 and 45) are divisible by 3 so start with length 7
	cands = ([int("".join(p)) for p in permutations("1234567")])[::-1]
	
	for cand in cands:
		if not cand % 10 in last_digits: continue
		if euler_util.is_prime(cand): return cand
Esempio n. 10
0
def euler58():
    """
	Starting with 1 and spiralling anticlockwise in the following way, a square
	spiral with side length 7 is formed.

	37 36 35 34 33 32 31
	38 17 16 15 14 13 30
	39 18  5  4  3 12 29
	40 19  6  1  2 11 28
	41 20  7  8  9 10 27
	42 21 22 23 24 25 26
	43 44 45 46 47 48 49

	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%?
	"""
    seed = 1
    side_length = 1
    ratio = 1.0
    primes = 0
    total = 1.0

    while ratio >= 0.1:
        side_length += 2
        inc = side_length - 1

        for i in range(1, 5):
            cand = seed + (i * inc)
            if euler_util.is_prime(cand): primes += 1

        total += 4
        seed += inc * 4
        ratio = primes / total

    return side_length
Esempio n. 11
0
def euler58():
	"""
	Starting with 1 and spiralling anticlockwise in the following way, a square
	spiral with side length 7 is formed.

	37 36 35 34 33 32 31
	38 17 16 15 14 13 30
	39 18  5  4  3 12 29
	40 19  6  1  2 11 28
	41 20  7  8  9 10 27
	42 21 22 23 24 25 26
	43 44 45 46 47 48 49

	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%?
	"""
	seed = 1
	side_length = 1
	ratio = 1.0
	primes = 0
	total = 1.0
	
	while ratio >= 0.1:
		side_length += 2
		inc = side_length - 1
		
		for i in range(1, 5):
			cand = seed + (i * inc)
			if euler_util.is_prime(cand): primes += 1
			
		total += 4
		seed += inc * 4
		ratio = primes / total
		
	return side_length
Esempio n. 12
0
def euler131(upper_bound=10**6):
    """http://projecteuler.net/problem=131

    There are some prime values, p, for which there exists a positive integer,
    n, such that the expression n**3 + pn**2 is a perfect cube.

    For example, when p = 19, 83 + 82*19 = 123.

    What is perhaps most surprising is that for each prime with this property
    the value of n is unique, and there are only four such primes below
    one-hundred.

    How many primes below one million have this remarkable property?
    """
    # Since n**3 + p*n**2 = (n+p)*n**2 with p prime, n and n+p are both
    # perfect cubes. So, this is just an alternative formulation for Cuban
    # primes: http://oeis.org/A002407
    #
    # These are also identical to the centered hexagonal numbers that are
    # also prime.
    hexes = [3*n*(n+1)+1 for n in xrange(int(upper_bound**0.5))]
    hexes = [n for n in hexes if n < upper_bound]
    return len([1 for n in hexes if is_prime(n)])
Esempio n. 13
0
from euler_util import primes_sieve, is_prime, is_odd
from math import sqrt



def is_sum_of_a_prime_and_twice_a_square(number):
    return len([i for i in primes_sieve(number - 2) if sqrt((number - i)/2.0).is_integer()]) > 0

print [i for i in range(1, 10000) if is_odd(i) and not is_prime(i) and not is_sum_of_a_prime_and_twice_a_square(i)]

Esempio n. 14
0
def concat_prime(p1, p2):
    if not is_prime(int(str(p1)+str(p2))): return False
    if not is_prime(int(str(p2)+str(p1))): return False
    return True
Esempio n. 15
0
def primos_entre(i, f):
    return [i for i in range(i, f + 1) if is_prime(i)]
Esempio n. 16
0
from euler_util import is_prime

def is_truncatable(n, leftToRight):
	if is_prime(n):
		if len(str(n)) == 1:
			return True
		if leftToRight:
			return is_truncatable(int(str(n)[1:]), True)
		else:
			return is_truncatable(int(str(n)[:-1]), False)
	else:
		return False

if __name__ == "__main__":
	p = 7
	s = 0
	n = 0
	target = 11
	while n < target:
		p += 1
		while not is_prime(p):
			p += 1
		if is_truncatable(p, True) and is_truncatable(p, False):
			s += p
			n += 1
	print s
Esempio n. 17
0
def divisores_primos(numero):
    return [i for i in divisores(numero) if is_prime(i)]
Esempio n. 18
0
from euler_util import is_prime

def completar_ceros(numero):
    temp = '0000'+str(numero)
    return temp[len(temp)-4:]

def ordernar_caracteres(string):
    return ''.join(sorted(string))

p = [i for i in range(3340)][1:]
s = map(lambda x : x+3330,p)
t = map(lambda x : x+3330,s)

sp = map(lambda x : ordernar_caracteres(completar_ceros(x)), p)
ss = map(lambda x : ordernar_caracteres(completar_ceros(x)), s)
st = map(lambda x : ordernar_caracteres(completar_ceros(x)), t)

for i in range(3339):
    if sp[i]==ss[i] and ss[i]==st[i] and is_prime(i+1)and is_prime(i+1+3330) and is_prime(i+1+3330*2):
        print i+1, i+1+3330, i+1+3330*2, sp[i], ss[i], st[i]
Esempio n. 19
0
from euler_util import is_prime


def completar_ceros(numero):
    temp = '0000' + str(numero)
    return temp[len(temp) - 4:]


def ordernar_caracteres(string):
    return ''.join(sorted(string))


p = [i for i in range(3340)][1:]
s = map(lambda x: x + 3330, p)
t = map(lambda x: x + 3330, s)

sp = map(lambda x: ordernar_caracteres(completar_ceros(x)), p)
ss = map(lambda x: ordernar_caracteres(completar_ceros(x)), s)
st = map(lambda x: ordernar_caracteres(completar_ceros(x)), t)

for i in range(3339):
    if sp[i] == ss[i] and ss[i] == st[i] and is_prime(i + 1) and is_prime(
            i + 1 + 3330) and is_prime(i + 1 + 3330 * 2):
        print i + 1, i + 1 + 3330, i + 1 + 3330 * 2, sp[i], ss[i], st[i]
Esempio n. 20
0
from euler_util import is_prime

def primos_entre(i,f):
    return [i for i in range(i,f+1) if is_prime(i)]

#b = primos_entre(2, 1000) 
#c = map(lambda x : x*-1,b)
if __name__ == '__main__':
    max  = 0
    for a in range(-999,999+1):
        for b in primos_entre(-1000,1000):
            n = 1
            while is_prime(n**2+a*n+b):
                n= n +1
            if n > max:
                maxa =a
                maxb=b
                max = n

print maxa, maxb, maxa * maxb 
Esempio n. 21
0
from euler_util import is_prime


def primos_entre(i, f):
    return [i for i in range(i, f + 1) if is_prime(i)]


#b = primos_entre(2, 1000)
#c = map(lambda x : x*-1,b)
if __name__ == '__main__':
    max = 0
    for a in range(-999, 999 + 1):
        for b in primos_entre(-1000, 1000):
            n = 1
            while is_prime(n**2 + a * n + b):
                n = n + 1
            if n > max:
                maxa = a
                maxb = b
                max = n

print maxa, maxb, maxa * maxb
Esempio n. 22
0
from euler_util import primes_sieve, is_prime, is_odd
from math import sqrt


def is_sum_of_a_prime_and_twice_a_square(number):
    return len([
        i for i in primes_sieve(number - 2)
        if sqrt((number - i) / 2.0).is_integer()
    ]) > 0


print[
    i for i in range(1, 10000) if is_odd(i) and not is_prime(i)
    and not is_sum_of_a_prime_and_twice_a_square(i)
]
Esempio n. 23
0
from euler_util import is_prime

print 2+sum([2*x+1 for x in range(1, 1000000) if is_prime(2*x+1)])
Esempio n. 24
0
def primos_entre(i,f):
    return [i for i in range(i,f+1) if is_prime(i)]