Ejemplo n.º 1
0
def solve():
    truncatable_primes = []

    i = 1
    while len(truncatable_primes) < 11:
        if is_prime(i):
            if i > 10:
                str_i = str(i)
                truncated_combinations = []
                if len(str_i) >= 2:

                    # Get all forward/backward combinations
                    for pos, n in enumerate(str_i, start=1):
                        if pos == len(str_i):
                            break
                        truncated_combinations.append(str_i[pos:])
                        truncated_combinations.append(str_i[:-pos])

                    # Check if these numbers are all primes
                    can_add = not (not truncated_combinations) # False when list is empty
                    for n in truncated_combinations:
                        if not is_prime(int(n)):
                            can_add = False
                            break
                    if can_add:
                        truncatable_primes.append(i)
        i += 1

    print sum(truncatable_primes)
Ejemplo n.º 2
0
def find_nth_prime(n):
    prime_list = set()
    x = 1
    while len(prime_list) < n:
        if is_prime(x):
            prime_list.add(x)
        x += 1
    return max(prime_list)
Ejemplo n.º 3
0
 def test_problem_3(self):
     from p003 import is_prime, func
     self.assertEqual(is_prime(0), False)
     self.assertEqual(is_prime(1), False)
     self.assertEqual(is_prime(2), True)
     self.assertEqual(is_prime(3), True)
     self.assertEqual(is_prime(4), False)
     self.assertEqual(is_prime(5), True)
     self.assertEqual(is_prime(15), False)
     self.assertEqual(is_prime(17), True)
     self.assertEqual(func(14), 7)
     self.assertEqual(func(13195), 29)
     self.assertEqual(func(600851475143), 6857)
Ejemplo n.º 4
0
def find_prime_factorization(number):
    all_factors = find_factors(number)
    prime_factorization = []
    if len(all_factors) == 1:
        prime_factorization.extend([x for x in all_factors[0]])
    else:
        all_factors.remove((1, number))
        factor_pair = all_factors.pop()

        for f in factor_pair:
            if is_prime(f):
                prime_factorization.append(f)
            elif f not in (1, number):
                map(lambda x: prime_factorization.append(x), find_prime_factors(f))
    return prime_factorization
Ejemplo n.º 5
0
import math
import time
from p003 import is_prime

if __name__ == '__main__':
    start_time = time.time()

    primes = [
        2
    ]  # initialize a list of prines collected. Start with first prime is 2
    x = 10001  # want to find the xth prime
    n = 3  # start at this

    while len(
            primes
    ) < x:  # is the nth number on the list yet? +1 because array starts at zero
        if is_prime(n):
            primes.append(n)  # adds that prime number to the list
        n += 1  # increment to the next number

    print("--- %s seconds ---" % (time.time() - start_time))
    print(primes[-1])