Exemplo n.º 1
0
def solve(count: int = 10001) -> int:
    """Solves problem 7.

    Counts prime numbers up to the count and stops
    counting once the function has counted primes
    equaling the passed in count.

    Arguments:
        count {int} -- The number of prime numbers to count.
        Default is 10001.

    Returns:
        int -- The last prime number that was counted.
    """
    if count <= 0:
        return 0
    if count == 1:
        return 2
    if count == 2:
        return 3

    n_odd = 5
    primes_counted = 3
    while primes_counted < count:
        n_odd += 2
        if is_prime(n_odd):
            primes_counted += 1
    return n_odd
Exemplo n.º 2
0
    def test_is_prime_with_lte_1(self):
        """Test that False is returned when the number is a number less than or equal to 1."""
        not_primes = [-1, 0, 1]
        for not_prime in not_primes:
            with self.subTest(i=not_prime):
                number = not_prime

                actual = is_prime(number)

                self.assertFalse(actual)
Exemplo n.º 3
0
    def test_is_prime_with_primes(self):
        """Test that is_prime returns True for the first primes less than 20."""
        known_primes = [2, 3, 5, 7, 11, 13, 17, 19]
        for prime in known_primes:
            with self.subTest(i=prime):
                number = prime

                actual = is_prime(number)

                self.assertTrue(actual)
Exemplo n.º 4
0
    def test_is_prime_with_not_primes(self):
        """Test that is_prime returns False for non-prime numbers less than 10."""
        not_primes = [4, 6, 8, 9]
        for not_prime in not_primes:
            with self.subTest(i=not_prime):
                number = not_prime

                actual = is_prime(number)

                self.assertFalse(actual)
Exemplo n.º 5
0
def solve(limit: int = 2000000) -> int:
    """Solves problem 10.

    Computes the sum of all prime numbers less than the limit.
    Each number is tested for primality and added to a running
    sum if the number is prime.

    Arguments:
        limit {int} -- Integers less than this number will be tested for primality.
        Default is 2000000.

    Returns:
        int -- The sum of all primes less than the limit.
    """
    # TODO: Runs at ~13 sec when limit is 2 mil. Needs optimization.
    counter = 0
    result = 0
    while counter < limit:
        if is_prime(counter):
            result += counter
        counter += 1
    return result
Exemplo n.º 6
0
def solve(integer: int = 600851475143) -> int:
    """Solves problem 3.

    Finds the largest prime of the argument
    through trial division by checking
    if the factor is a prime. The factors are
    checked in reverse order and starts with
    the square root of the integer.

    Arguments:
        integer {int} -- The integer to be factored.
        Default is 600851475143

    Returns:
        int -- The largest prime factor of the integer if found.
    """
    prime = None
    upper_factor = ceil(sqrt(integer))
    for i in range(upper_factor, 0, -1):
        if integer % i == 0 and is_prime(i):
            prime = i
            break
    return prime