def problem51(): primes = primesUpTo(10 ** 7) for p in primes[1229:]: m = str(p) for i in ['0', '1', '2']: if m.count(i) == 3 and primes8(m, i): return p
def problem35(): from PE_primes import primesUpTo, isPrime # count is 1 is we are not count 2 later count = 1 # remove all primes that contain an even number, since those will have a cyclic shift that is even primesList = {p for p in primesUpTo(10**6) if isMadeOfOdd(p)} for p in primesList: if all(q in primesList for q in iCyclicShifts(p)): count += 1 return count
def problem49(): primes = primesUpTo(10000) for a in dropwhile(lambda x: x <= 1487, primes): # the bound comes from wanting c = 2b - a ≤ 10000 for b in dropwhile(lambda x: x <= a, primes): if b >= (10000 + a) / 2: break c = b + (b - a) # Do the same digits first since I think that is the most time # consuming part if sameDigits(a, b) and sameDigits(b, c) and isPrime(c): return int(str(a) + str(b) + str(c))
def problem70(): GOAL = 10 ** 7 primes = primesUpTo(2 * int(GOAL ** (1 / 2))) record = 1000000000 for index, p in enumerate(primes[1:]): for q in primes[index:]: if q > GOAL // p: break n = p * q ph = (p - 1) * (q - 1) if isPermutation(n, ph) and n / ph < record: record = n / ph recordN = n return recordN
def problem50(): GOAL = 10**6 listOfPrimes = primesUpTo(GOAL) recordLength = 21 maxPrime = 953 for index in count(): for length, running in enumerate(accumulate(listOfPrimes[index:])): if length + 1 > recordLength and isPrime(running): recordLength = length + 1 maxPrime = running if running > GOAL: break # If there is no hope, break out if sum(listOfPrimes[index: index+recordLength]) > GOAL: break return maxPrime
def problem46(): primesList = primesUpTo(10 ** 4) for l in range(1, 10 ** 4): n = 2 * l + 1 flag = True if not (n in primesList): a = 1 while 2 * a ** 2 < n: q = n - 2 * a ** 2 if q in primesList: flag = False break a += 1 else: flag = False if flag: return n
def problem27(): record = 0 ab = 0 checker = Orderedlist(primesUpTo(1998)) # We know there is a record of 80 # this removes many possibilities for low values of b # since the most number of primes for f(n) is b-1 for b in checker[80:1000]: for p1 in checker[0:1000 + b + 1]: a = p1 - b - 1 n = 2 p2 = a + p1 + (2 * n - 1) while isPrime(abs(p2)): n += 1 p2 = a + p2 + (2 * n - 1) if n > record: record = n ab = a * b return ab
def problem7(): ''' We have a crude bound on the number of primes given by x/ln(x) Thus we need 10001 < x/ln(x) and 200000 works''' primes = primesUpTo( 200000) return primes[10001-1]
def problem10(): return sum(primesUpTo(2*10**6))