Ejemplo n.º 1
0
def is_truncatable(num: int) -> bool:
    if not primes.is_prime(num):
        return False

    mult = 10
    while mult < num:
        if not primes.is_prime(num % mult) or not primes.is_prime(num // mult):
            return False
        mult *= 10

    return True
Ejemplo n.º 2
0
def solution():
    t_primes = []
    left_right = [3, 7]
    right_left = [2, 5, 3, 7]
    while len(t_primes) < 11:
        left_right = [
            add_digit(n, d, 1) for n in left_right for d in range(1, 10)
        ]
        right_left = [
            add_digit(n, d, 0) for n in right_left for d in range(1, 10)
        ]
        left_right = [p for p in left_right if is_prime(p)]
        right_left = [p for p in right_left if is_prime(p)]
        t_primes += [p for p in left_right if p in right_left]
    return sum(t_primes)
Ejemplo n.º 3
0
def problem_50():

    limit = 10**6

    # find the smallest list of primes which will sum up to at least the limit we set above
    for x in count(1):
        primes = list(get_primes_under(10**x))
        if sum(primes) > limit:
            break

    # starting with the longest possible sequence (every element in the list), and then working
    # to smaller sequence lengths
    for length in reversed(range(1, len(primes) + 1)):

        # Use a "sliding window" approach. Starting at the low end of the list, grab the first
        # `length` elements and check it. Then slide the window right 1 element, and grab those,
        # and check it, etc
        for start in range(len(primes)-length):
            candidate = sum(primes[start:start+length])

            # if the candidate sum is bigger than the limit, no point in checking any more windows
            # of size `length`. Break the inner loop, and reduce the length
            if candidate >= limit:
                break

            # if the candidate sum is prime, we're done!
            if is_prime(candidate):
                print(candidate)
                return
Ejemplo n.º 4
0
def nth_prime(n):
    """Returns nth prime"""
    current = 1
    primes = 0
    while primes < n:
        current += 1
        if is_prime(current):
            primes += 1
    return current
Ejemplo n.º 5
0
def check(num):
	if '200' not in str(num):
		return False
	for i in xrange(len(str(num))):
		for j in range(0, 10):
			new_num = int(str(num)[:i] + str(j) + str(num)[i + 1:])
			if is_prime(new_num):
				return False
	return True
Ejemplo n.º 6
0
def is_circular_prime(n):
    """ Returns True is n is a circular prime: that is, if n itself is prime, as are all rotations
    of its digits. """

    # if n itself is not prime, it's certainly not a circular prime
    if not is_prime(n):
        return False

    # convert n to a list of digits so we can rotate it
    n = list(str(n))

    # For every rotation of the digits of n, convert that back into an int, and check primality
    # If any of them are not, return False
    rotated_number = lambda list, i: int(''.join(rotate(list, i)))
    if any(not is_prime(rotated_number(n, i)) for i in range(1, len(n))):
        return False

    # We made it here, so n and all rotations of n are prime
    return True
Ejemplo n.º 7
0
def solution():
    # primes_list = seive_of_erat(1000000000)
    # primes_set = set(primes_list)
    n = 1
    all_corners = 5
    corner_primes = 3
    while corner_primes / all_corners >= 0.1:
        n += 1
        end = (2*n + 1) * (2*n + 1)
        all_corners += 4
        corner_primes += len([1 for c in get_corners(n) if is_prime(c)])
    return 2*n + 1
Ejemplo n.º 8
0
def solution():
    digits = [1, 2, 3, 4, 5, 6, 7]
    best = 0
    current = digits
    while len(digits) < 10:
        conc = int("".join([str(d) for d in current]))
        if is_prime(conc):
            best = conc
        current = next_perm(current)
        if not current:
            digits.append(digits[-1] + 1)
            current = digits
    return best
Ejemplo n.º 9
0
def main():
    prime_count = 0
    total_count = 1
    num = 1

    for side_length in itertools.count(3, 2):
        for _ in range(4):
            total_count += 1

            num += side_length - 1

            if primes.is_prime(num):
                prime_count += 1

            if prime_count / total_count < .1:
                return side_length
Ejemplo n.º 10
0
def can_be_written_as_prime_plus_twice_square(n):
    """ Returns True if n can be written as the sum of a prime and twice a square number, or
    returns False otherwise. """

    if is_prime(n):
        return True

    # For every prime under n, find what value of x satisfies:   n = prime + 2x^2
    # If any of those values x are an integer, then n can be written as prime + 2x square number
    for prime in get_primes_under(n):
        x = ((n - prime) / 2) ** 0.5
        if x % 1 == 0:
            return True

    # If we get here, n cannot be written as prime + 2x square number
    return False
Ejemplo n.º 11
0
def count_primes_of_quadratic(a, b):
    """ Returns the number of consecutive primes produced by the quadratic equation n² + an + b,
    with n starting at 0. """

    n = 0
    count = 0

    while True:
        if is_prime(n*n + a*n + b):
            count += 1
            n += 1

        else:
            break

    return count
Ejemplo n.º 12
0
 def ana_solve(self):
     solution = 0
     primeList = [5, 7, 11, 13, 17, 19, 23]
     combos = combinations(primeList, 3)
     primeCombos = []
     for c in combos:
         s = sum(c)
         if primes.is_prime(s):
             primeCombos.append((c, s))
     counts = Counter([pc[1] for pc in primeCombos])
     bigC = []
     for c in counts:
         if counts[c] >= 4:
             bigC.append(c)
     candidates = []
     for pc in primeCombos:
         if pc[1] in bigC:
             candidates.append(pc)
             candidates.sort(key=lambda x: x[1])
     print(candidates)
     print('analytical solution = ', solution)
Ejemplo n.º 13
0
def problem_58():

    # These define the north-east and the south-west spokes, for consecutive n starting at n=0
    ne_spoke = lambda n: (2*n + 1)**2
    sw_spoke = lambda n: 4*(n**2) + 1

    # These define the north-west and the south-east spokes, for consecutive n starting at n=1
    nw_spoke = lambda n: 4*(n**2) - 6*n + 3
    se_spoke = lambda n: 4*(n**2) - 10*n + 7

    all_spoke_values = set()
    prime_spoke_values = set()

    for n in count(0):
        for value in (ne_spoke(n), sw_spoke(n), nw_spoke(n+1), se_spoke(n+1)):
            all_spoke_values.add(value)
            if is_prime(value):
                prime_spoke_values.add(value)

        if n > 1 and (len(prime_spoke_values) / len(all_spoke_values)) < 0.10:
            spiral_side_length = 2*n + 1
            print(spiral_side_length)
            break
Ejemplo n.º 14
0
def concats_to_prime(p1, p2):
    """ Check if both concatenations of primes p1 and p2 (p1+p2, and p2+p1), form a prime. """

    p1, p2 = str(p1), str(p2)
    t1, t2 = int(p1 + p2), int(p2 + p1)
    return all(is_prime(t) for t in (t1, t2))
Ejemplo n.º 15
0
# It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

# 9 = 7 + 2×1**2
# 15 = 7 + 2×2**2
# 21 = 3 + 2×3**2
# 25 = 7 + 2×3**2
# 27 = 19 + 2×2**2
# 33 = 31 + 2×1**2

# It turns out that the conjecture was false.

# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

from utils.primes import primesfrom2to, is_prime
num = 1
while True:
	found = False
	num += 2
	if is_prime(num):continue
	pl = primesfrom2to(num)
	for p in pl:
		for i in range(1, int(((num - p) / 2)**0.5) + 1):
			if num == p + 2 * i**2:
				found = True
				break
		if found:
			break
	if not found:
		print(num)
		break
Ejemplo n.º 16
0
def is_circular_prime(num: int) -> bool:
    for rotation in rotate(num):
        if not is_prime(rotation):
            return False

    return True
Ejemplo n.º 17
0
 def test_evens_are_not_prime(self):
     self.assertFalse(is_prime(4))
     self.assertFalse(is_prime(8))
Ejemplo n.º 18
0
 def test_2_is_prime(self):
     self.assertEquals(True, is_prime(2))
Ejemplo n.º 19
0

# 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?

from utils.primes import is_prime
from itertools import permutations

ans = 0
p = ['1','2','3']
for i in range(4,10):
	p += [str(i)]
	for s in permutations(p):
		v = int(''.join(s))
		if is_prime(v):
			# print(v)
			ans = max(ans, v)
print(ans)
Ejemplo n.º 20
0
 def test_13_is_prime(self):
     self.assertTrue(is_prime(13))
Ejemplo n.º 21
0
# There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

# What 12-digit number do you form by concatenating the three terms in this sequence?
from itertools import permutations
from utils.primes import primesfrom2to, is_prime

found = False
for p in primesfrom2to(10000):
	if p < 1000:continue
	perms = tuple(permutations(list(str(p))))
	if ('1','4','8','7') in perms:continue
	for s1 in perms:
		n1 = int(''.join(s1))
		if n1 < 1000:continue
		if not is_prime(n1):continue
		for s2 in perms:
			if s2 == s1:continue
			n2 = int(''.join(s2))
			if n2 < 1000:continue
			if not is_prime(n2):continue
			for s3 in perms:
				if s3 == s1 or s3 == s2:continue
				n3 = int(''.join(s3))
				if n3 < 1000:continue
				if not is_prime(n3):continue
				if abs(n1 - n2) == abs(n2 - n3):
					found = True
				if found:break
			if found:break
		if found:break
Ejemplo n.º 22
0
# 15 = 3 × 5

# The first three consecutive numbers to have three distinct prime factors are:

# 644 = 2**2 × 7 × 23
# 645 = 3 × 5 × 43
# 646 = 2 × 17 × 19.

# Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?

from utils.primes import is_prime
from utils.common import prime_factors

num = 2 * 3 * 5 * 7
while True:
	if is_prime(num+3):
		num += 4
		continue
	if is_prime(num+2):
		num += 3
		continue
	if is_prime(num+1):
		num += 2
		continue
	n1 = len(set(prime_factors(num))) == 4
	n2 = len(set(prime_factors(num+1))) == 4
	n3 = len(set(prime_factors(num+2))) == 4
	n4 = len(set(prime_factors(num+3))) == 4
	if n1 and n2 and n3 and n4:
		print(num)
		break
Ejemplo n.º 23
0
def main():
    for x in itertools.count(9, 2):
        if not primes.is_prime(x) and not is_goldbach_num(x):
            return x
Ejemplo n.º 24
0
 def test_negatives_are_notprime(self):
     self.assertFalse(is_prime(-3))
     self.assertFalse(is_prime(-7))
Ejemplo n.º 25
0
# , where |a|<1000 and |b|≤1000

# where |n|
# is the modulus/absolute value of n
# e.g. |11|=11 and |−4|=4

# Find the product of the coefficients, a
# and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.

from utils.primes import is_prime

rangea = 1000
rangeb = 1001
max_n = 0
for a in range(-rangea, rangea):
	for b in range(-rangeb, rangeb):
		n = 0
		while True:
			num = n**2 + a * n + b
			if not is_prime(num):
				if n >= max_n:
					max_n = n
					max_a = a
					max_b = b
					# print(max_a, max_b, max_n)
				break
			n += 1
# print(max_a, max_b, max_n)
print(max_a * max_b)