def problem111a(): # 9 digits the same # 1 digit is the same total = 0 for digits in [str(i) for i in range(0,10)]: count = 0 for index in range(0,10): for a in range(0,10): number = [digits]*9 number.insert(index,str(a)) a = int(''.join(number)) if len(str(a)) == 10 and isPrime(a): total += a #print(a) for digits in ['2','8','0']: count = 0 for index1,index2 in combinations([i for i in range(0,10)],2): for a,b in itertools.product(range(10),range(10)): number = [digits]*10 number[index1] = str(a) number[index2] = str(b) a = int(''.join(number)) if len(str(a)) == 10 and isPrime(a): total += a return total
def problem146(): total = 0 for i in range(10, 150000000 + 1, 10): n = i ** 2 if n % 3 != 1: continue if n % 7 != 2 and n % 7 != 3: continue if n % 9 == 0 or n % 13 == 0 or n % 27 == 0: continue if all(isPrime(n + j) for j in [1, 3, 7, 9, 13, 27]) and not any(isPrime(n + j) for j in [19, 21]): print(i) total += i return total
def problem41(): from itertools import permutations from PE_primes import isPrime # Let's just go over all the 7 digits pandigital numbers in reverse and find the first one that is prime for c in permutations(range(7,0,-1),7): if isPrime(numberFromList(c)): return numberFromList(c)
def isPrimeProofsqubeEven(p, q): n = p ** 2 * q ** 3 if "200" not in str(n): return False for i in range(1, 10, 2): # the last digit is odd if isPrime(int(str(n)[:-1] + str(i))): return False return True
def problem249(): primes = primesUpTo(5000) sums = defaultdict(int) sums[0] = 1 for p,total in runningTotal(primes): for j in range(total,p-1,-1): sums[j] = sums[j] + sums[j-p] return sum([sums[k] for k in range(total+1) if isPrime(k)]) % 10**16
def primesInNbd(n, m): # Check the top and bottom row as the elements left and right cannot be primes # n,m is a prime number locations = [] for j in [-1, 1]: for i in [-1, 0, 1]: if isPrime(a(n + j, m + i)): locations.append((n + j, m + i)) return locations
def isPrimeProofsqubeOdd(p, q): n = p ** 2 * q ** 3 if "200" not in str(n): return False for digit in range(len(str(n))): for i in range(0, 10): m = str(n)[:digit] + str(i) + str(n)[digit + 1:] if isPrime(int(m)): return False return True
def problem131a(): count = 0 for a in range(1,10**5): a3 = a**3 factorsOfa = factorize(a) div = divisorsFromFactors( factorsOfa + factorsOfa + factorsOfa ) for k in div: n = isSquare(a3 // k) if n and k-n >0 and isPrime(k - n): print(k-n)
def problem131(): count = 0 primes = set() for a in range(1,10**10): for n in range(1,a+1): if 0 < (a**3 - n**3) / n**2 < 10**6 and (a**3 - n**3) % n**2 == 0 and isPrime((a**3 - n**3) // n**2): count += 1 print((a**3 - n**3) // n**2, len(primes), a,n) primes.add((a**3 - n**3) // n**2) break
def allPrimes(digits,soFar=set()): if len(digits) == 0: seen.add(tuple(sorted(tuple(soFar)))) print(len(seen)) else: for combi in powerset(digits): if combi == (): continue for perm in permutations(combi, len(combi)): number = int(''.join([str(s) for s in perm])) if isPrime(number): allPrimes(digits.difference(perm), soFar.union({number}))
def S(n): total = 0 previousIsPrime = False # A small optimization that doesn't do much for m in range(1, n + 1): if previousIsPrime: previousIsPrime = False continue if isPrime(a(n, m)): previousIsPrime = True if isPrimeTriplet(n, m): total += a(n, m) return total
def problem146(): total = 0 # n % 210 = 10 for n in range(10, 15 * 10 ** 7, 210): if all([isPrime(n ** 2 + i) for i in [1, 3, 7, 9, 13, 27]]): print(n) total += n # n % 210 = 80 for n in range(80, 15 * 10 ** 7, 210): if all([isPrime(n ** 2 + i) for i in [1, 3, 7, 9, 13, 27]]): print(n) total += n # n % 210 = 130 for n in range(130, 15 * 10 ** 7, 210): if all([isPrime(n ** 2 + i) for i in [1, 3, 7, 9, 13, 27]]): print(n) total += n # n % 210 = 200 for n in range(200, 15 * 10 ** 7, 210): if all([isPrime(n ** 2 + i) for i in [1, 3, 7, 9, 13, 27]]): print(n) total += n return total
def problem131(): seen = 0 for m in count(2): factors = set(factorize(m)) n = m**3 for k in range(0,n,product(factors)**2): if any( k % p != 0 for p in factors): continue if k % product(factors) != 0: print(k) if (n**3 - k**3) % n**2 == 0 and isPrime((n**3 - k**3) // n**2): p = (n**3 - k**3) // n**2 if p > 10**6: return seen print(n, p, k) seen += 1
def problem216(): count = 0 for p in iPrime(): print(jacobi(( p + 1)//2, p)) if p > 100: break return for index, n in enumerate(t(100)): index += 2 #print( 2*(index)**2 -1 , n, isPrime(n), jacobi(( index + 1)//2, index)) if isPrime(n): count += 1 print("gave a prime ", jacobi(( n + 1)//2, n)) else: for p in factorize(n): print("Didn't give a prime", p,jacobi(( p + 1)//2, p)) return count
def problem293(): GOAL = 10**9 primes = [] maximum = 1 for p in iPrime(): maximum *= p primes.append(p) # need one more beyond what is required if maximum > GOAL: break admissible = [] total = 0 num = 0 for c in generateAdmissible(GOAL,primes): #print(c, sorted(factorize(c))) if c > GOAL: continue num += 1 for d in count(3,2): if isPrime(c+d): total += d break return num, total
def problem128(): count = 1 limit = 2000 n = 0 number = 0 while count < limit: n += 1 if (isPrime(6 * n - 1) and isPrime(6 * n + 1) and isPrime(12 * n + 5)): count += 1; number = (3 * n * n - 3*n + 2) if (count >= limit): break if (isPrime(6 * n + 5) and isPrime(6 * n - 1) and isPrime(12 * n - 7) and n != 1): count += 1 number = (3 * n * n + 3*n + 1) return number