Esempio n. 1
0
class Problem58:
    
    def __init__(self):
        self.primeChecker = PrimeChecker()
        self.primeChecker.initPrimeFactoring()

    def getDiagonalsFromSpiralOfSize(self, size, diagonals=None):
        counter = 1
        if diagonals == 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
        totalPrimes = 0
        while ratio >= 0.10:
            size += 2
            diagonals = self.getDiagonalsFromSpiralOfSize(size, diagonals)
            total = (size-1)/2 * 4 + 1
            totalPrimes += len([x for x in diagonals[-4:-1] if self.primeChecker.isPrime(x)])
            ratio = totalPrimes / total
        return size
            
Esempio n. 2
0
class Problem47:
    def __init__(self):
        self.primeChecker = PrimeChecker()
        self.primeChecker.initPrimeFactoring()
    
    def findConsecutivePrime(self, n):
        i = 1
        while True:
            for j in range(0, n):
                if not len(self.primeChecker.getUniquePrimeFactors(i, True)) == n:
                    break
                i += 1
                if j == n - 1:
                    return i-n
            i += 1
        
    
    def answer(self):
        return self.findConsecutivePrime(4)
Esempio n. 3
0
class Problem60:
    def __init__(self, primeLimit):
        self.primeChecker = PrimeChecker()
        self.primeChecker.initPrimeFactoring()
        self.primes = PrimeChecker.primesBelow(primeLimit)
        self.primes.remove(2)
        self.pairs = self.buildPairs(self.primes)
    
    def buildPairs(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.isPrime(int(str(p1) + str(p2))) and \
                self.primeChecker.isPrime(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]:
                setA = self.pairs[p1] & self.pairs[p2]
                if len(setA) > 0:
                    for p3 in setA:
                        setB = setA & self.pairs[p3]
                        if len(setB) > 0:
                            for p4 in setB:
                                setC = setB & self.pairs[p4]
                                if len(setC) > 0:
                                    total = sum([p1, p2, p3, p4] + list(setC))
                                    if total < lowest:
                                        lowest = total
        return lowest
Esempio n. 4
0
class TestProblem47(unittest.TestCase):

    def setUp(self):
        self.primeChecker = PrimeChecker()
        self.primeChecker.initPrimeFactoring()
        pass
    
    def tearDown(self):
        pass

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

    def test_27ShouldNotBeAPrime(self):
        self.assertFalse(PrimeChecker.isPrime(27))
        
    def test_primesBelow20ShouldBe235711131719(self):
        self.assertEquals([2,3,5,7,11,13,17,19], PrimeChecker.primesBelow(20))
        
    def test_primeFactorsFor644ShouldBe22723(self):
        self.assertEquals([2,2,7,23], self.primeChecker.getPrimeFactors(644, True))
        
    def test_uniquePrimeFactorsFor644ShouldBe2723(self):
        self.assertEquals([2,7,23], self.primeChecker.getUniquePrimeFactors(644, True))