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
def get_longest_sum_of_consecutive_primes_below(n): longest_sequence_length = 0 longest_sum = 0 primes = PrimeChecker.primes_below(n) for i in range(0, len(primes)): for j in range(i + 1, len(primes) - 1): prime_sum = sum(primes[i:j]) if prime_sum >= n: break if PrimeChecker.is_prime(prime_sum) and j - i > longest_sequence_length: longest_sequence_length = j - i longest_sum = prime_sum return longest_sequence_length, longest_sum
def getLongestSumOfConsecutivePrimesBelow(self, n): longestSequenceLength = 0 longestSum = 0 primes = PrimeChecker.primesBelow(n) for i in range(0, len(primes)): for j in range(i+1, len(primes)-1): primeSum = sum(primes[i:j]) if primeSum >= n: break if PrimeChecker.isPrime(primeSum) and j - i > longestSequenceLength: longestSequenceLength = j - i longestSum = primeSum return longestSequenceLength, longestSum
def triSequenceIsPrimeAndPermutation(self, start, step): for n in range(0, 3): if not PrimeChecker.isPrime(start+n*step): return False for n in range(0, 2): if not self.isPermutationOf(n, start): return False return True
def hasPrimeVariations(self, number, replaceDigit, primeVariations): primeCount = 0 for i in range(0, 10): testNumber = number.replace(replaceDigit, str(i)) if PrimeChecker.isPrime(int(testNumber)) \ and self.hasEqualNumberOfDigits(testNumber, number): primeCount += 1 return primeCount == primeVariations
def tri_sequence_is_prime_and_permutation(self, start, step): for n in range(0, 3): if not PrimeChecker.is_prime(start + n * step): return False for n in range(0, 3): if not self.is_permutation_of(start + n * step, start): return False return True
def answer(self): for prime in PrimeChecker.primesBelow(1000000): if prime > 100000: s = str(prime) lastDigit = s[5:6] if s.count("0") == 3 and self.hasPrimeVariations(s, "0", 8) \ or s.count("1") == 3 and lastDigit != 1 and self.hasPrimeVariations(s, "1", 8) \ or s.count("2") == 3 and self.hasPrimeVariations(s, "2", 8): return int(s)
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)
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
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))
def setUp(self): self.primeChecker = PrimeChecker() self.primeChecker.initPrimeFactoring() pass
def test_primesBelow20ShouldBe235711131719(self): self.assertEquals([2,3,5,7,11,13,17,19], PrimeChecker.primesBelow(20))
def test_27ShouldNotBeAPrime(self): self.assertFalse(PrimeChecker.isPrime(27))
def test_47ShouldBeAPrime(self): self.assertTrue(PrimeChecker.isPrime(47))
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 __init__(self): self.primeChecker = PrimeChecker() self.primeChecker.initPrimeFactoring()
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)