Пример #1
0
class Problem58:
    
    def __init__(self):
        self.primeChecker = PrimeChecker()
        self.primeChecker.init_prime_factoring()

    @staticmethod
    def get_diagonals_from_spiral_of_size(size, diagonals=None):
        counter = 1
        if diagonals is None:
            diagonals = []
            for i in range(0, int((size-1)/2)):
                for _ in range(0, 4):
                    counter += 2 + 2*i
                    diagonals.append(counter)
        else:
            i = int((size-1)/2) - 1
            counter = int(diagonals[-1])
            for _ in range(0, 4):
                counter += 2 + 2*i
                diagonals.append(counter)
        return diagonals
    
    def answer(self):
        size = 1
        ratio = 100
        diagonals = None
        total_primes = 0
        while ratio >= 0.10:
            size += 2
            diagonals = self.get_diagonals_from_spiral_of_size(size, diagonals)
            total = (size-1)/2 * 4 + 1
            total_primes += len([x for x in diagonals[-4:-1] if self.primeChecker.is_prime(x)])
            ratio = total_primes / total
        return size
Пример #2
0
class Problem47:
    def __init__(self):
        self.primeChecker = PrimeChecker()
        self.primeChecker.init_prime_factoring()

    def find_consecutive_prime(self, n):
        i = 1
        while True:
            for j in range(0, n):
                if not len(self.primeChecker.get_unique_prime_factors(i, True)) == n:
                    break
                i += 1
                if j == n - 1:
                    return i - n
            i += 1

    def answer(self):
        return self.find_consecutive_prime(4)
Пример #3
0
class Problem60:
    def __init__(self, prime_limit):
        self.primeChecker = PrimeChecker()
        self.primeChecker.init_prime_factoring()
        self.primes = PrimeChecker.primes_below(prime_limit)
        self.primes.remove(2)
        self.pairs = self.build_pairs(self.primes)
    
    def build_pairs(self, primes):
        pairs = dict()
        for p1 in primes:
            if p1 == 5:
                continue
            pairs[p1] = set()
            for q in range(primes.index(p1), len(primes)):
                p2 = primes[q]
                if self.primeChecker.is_prime(int(str(p1) + str(p2))) and \
                        self.primeChecker.is_prime(int(str(p2) + str(p1))):
                    pairs[p1].add(p2)
        return pairs
    
    def answer(self):
        lowest = 10000000
        for p1 in self.pairs:
            for p2 in self.pairs[p1]:
                set_a = self.pairs[p1] & self.pairs[p2]
                if len(set_a) > 0:
                    for p3 in set_a:
                        set_b = set_a & self.pairs[p3]
                        if len(set_b) > 0:
                            for p4 in set_b:
                                set_c = set_b & self.pairs[p4]
                                if len(set_c) > 0:
                                    total = sum([p1, p2, p3, p4] + list(set_c))
                                    if total < lowest:
                                        lowest = total
        return lowest
Пример #4
0
class UtilsTest(unittest.TestCase):

    def setUp(self):
        self.primeChecker = PrimeChecker()
        self.primeChecker.init_prime_factoring()
        pass

    def tearDown(self):
        pass

    def test_47ShouldBeAPrime(self):
        self.assertTrue(PrimeChecker.is_prime(47))

    def test_27ShouldNotBeAPrime(self):
        self.assertFalse(PrimeChecker.is_prime(27))

    def test_primesBelow20ShouldBe235711131719(self):
        self.assertEqual([2, 3, 5, 7, 11, 13, 17, 19], PrimeChecker.primes_below(20))

    def test_primeFactorsFor644ShouldBe22723(self):
        self.assertEqual([2, 2, 7, 23], self.primeChecker.get_prime_factors(644, True))

    def test_uniquePrimeFactorsFor644ShouldBe2723(self):
        self.assertEqual([2, 7, 23], self.primeChecker.get_unique_prime_factors(644, True))