Ejemplo n.º 1
0
def problem35():
    """ Circular primes
    How many circular primes are there below one million?

    """
    return_value = 0
    for prime in mathhelper.prime_list(999999):
        prime_digits = list(str(prime))

        rotate_list = collections.deque(prime_digits)
        rotate_list_length = len(rotate_list)
        if rotate_list_length == 1:
            return_value += 1
            continue

        if len({"0", "2", "4", "5", "6", "8"}.intersection(prime_digits)) > 0:
            continue

        for rotate in range(1, rotate_list_length):
            rotate_list.rotate(1)
            if not mathhelper.is_prime(mathhelper.create_number(rotate_list)):
                break

            if rotate + 1 == rotate_list_length:
                return_value += 1

    return return_value
Ejemplo n.º 2
0
def problem3():
    """ Largest prime factor
    What is the largest prime factor of the number 600851475143 ?

    """
    const_number = 600851475143
    for i in range(int(math.sqrt(const_number)), 2, -1):
        if const_number % i == 0 and mathhelper.is_prime(i):
            return i
Ejemplo n.º 3
0
def problem46():
    """ Goldbach's other conjecture
    What is the smallest odd composite
    that cannot be written as the sum of a prime and twice a square?

    """
    number = 33
    while True:
        number += 2

        if mathhelper.is_prime(number):
            continue

        biggest_square = int(math.sqrt(number / 2))
        for power in range(2, biggest_square + 1):
            if mathhelper.is_prime(number - (2 * (power ** 2))):
                break

            if power == biggest_square:
                return number
Ejemplo n.º 4
0
def problem37():
    """ Truncatable primes
    Find the sum of the only eleven primes
    that are both truncatable from left to right and right to left.

    """
    found_item = 0
    return_value = 0
    for prime in mathhelper.prime_list(None):
        if prime < 10:
            continue

        prime_text = str(prime)
        for truncate in range(1, len(prime_text)):
            left_part = int(prime_text[:(-1 * truncate)])
            right_part = int(prime_text[truncate:])
            if not mathhelper.is_prime(left_part) or not mathhelper.is_prime(right_part):
                break
            if truncate + 1 == len(prime_text):
                return_value += prime
                found_item += 1

        if found_item == 11:
            return return_value
Ejemplo n.º 5
0
def problem7():
    """ 10001st prime
    What is the 10 001st prime number?

    """
    count = 0
    number = 1
    while True:
        number += 1

        if mathhelper.is_prime(number):
            count += 1

        if count == 10001:
            break
    return number
Ejemplo n.º 6
0
def problem50():
    """ Consecutive prime sum
    Which prime, below one-million, can be written as the sum of the most consecutive primes?

    """

    max_index = 0
    biggest_prime = 0
    for start in mathhelper.prime_list(1000000, reverse=True):
        sum_primes = 0
        for index, prime in enumerate(mathhelper.prime_list(start, reverse=True)):
            if sum_primes > 1000000:
                break

            sum_primes += prime

            if max_index < index and mathhelper.is_prime(sum_primes):
                max_index = index
                biggest_prime = sum_primes

    return biggest_prime
Ejemplo n.º 7
0
def problem10():
    """ Summation of primes
    Find the sum of all the primes below two million.

    """
    return sum(x for x in range(2, 2000000) if mathhelper.is_prime(x))
Ejemplo n.º 8
0
 def test_is_prime(self):
     self.assertEqual(mathhelper.is_prime(-5), False)
     self.assertEqual(mathhelper.is_prime(-4), False)
     self.assertEqual(mathhelper.is_prime(-3), False)
     self.assertEqual(mathhelper.is_prime(-2), False)
     self.assertEqual(mathhelper.is_prime(-1), False)
     self.assertEqual(mathhelper.is_prime(0), False)
     self.assertEqual(mathhelper.is_prime(1), False)
     self.assertEqual(mathhelper.is_prime(2), True)
     self.assertEqual(mathhelper.is_prime(3), True)
     self.assertEqual(mathhelper.is_prime(4), False)
     self.assertEqual(mathhelper.is_prime(5), True)
     self.assertEqual(mathhelper.is_prime(6), False)
     self.assertEqual(mathhelper.is_prime(7), True)