# Prime digit replacements import sys sys.path.append('../') from euler import listPrimes from itertools import combinations import time for d in range(2,4): s = time.time() maxVal = 0 print(d, 'DIGITS') lower, upper = 10**(d-1), 10**d - 1 primes = listPrimes(10**d) digitsToReplace = [i for i in range(d-1)] occ = {} for p in primes: if p < lower: continue num = list(str(p)) # number of digits to replace for n in range(1,d): # indices to replace
# Project Euler # Problem 160 # Factorial trailing digits import sys sys.path.append('../') import euler primes = euler.listPrimes(100) print(primes) def f(N): ans = 1 for n in range(1,N+1): while n % 10 == 0: n //= 10 ans *= n while ans % 10 == 0: ans //= 10 ans %= 1000000 print(str(n) + ' ' + str(ans)) return ans print(str(f(20))[-5:])
# Project Euler # Problem 41 # Pandigital prime import sys sys.path.append('../') from euler import listPrimes import numpy as np from itertools import permutations num = 987654321 primes = listPrimes(int(np.sqrt(num))) def isPrime(primes, n): result = True for i in primes: if i > np.sqrt(n): break if n % i == 0: result = False break return result print('starting....') ''' code takes long ish for 9 digits only need to check up to 7 digits because all 8- and 9-digit pandigital numbers are divisible by 3 (sum of digits divisible by 3) and hence not prime
# Project Euler # Problem 47 # Distinct primes factors import sys sys.path.append('../') from euler import listPrimes from math import sqrt searchMax = 1000000 primes = listPrimes(int(sqrt(searchMax))) target = 4 primeFacs = [[] for _ in range(target)] numPrimeFacs = [0] * target for n in range(2, searchMax): orig = n primeFacs.pop(0) numPrimeFacs.pop(0) primeFacs.append([]) i = 0 while primes[i] <= int(sqrt(n)): if n % primes[i] == 0: primeFacs[-1].append(primes[i]) n //= primes[i] while n % primes[i] == 0: n //= primes[i] i += 1
class Pattern(object): def __init__(self, string=''): self.string = string self.numbers = [] self.count = 0 def add(self, number): self.numbers.append(number) self.count += 1 def __repr__(self): return self.string + ': ' + ' '.join(map(str, self.numbers)) num_digits = 6 primes = listPrimes(10**num_digits) primes = [p for p in primes if len(str(p)) == num_digits] patterns = {} def num_to_pattern_string(n, to_replace): pattern = list(str(n)) for i in range(len(pattern)): if to_replace[i] == 1: pattern[i] = 'X' return ''.join(pattern) def compute_pattern(p, to_replace, digit): if to_replace[0] == 1 and digit == 0: return if sum(to_replace) == 0:
# Project Euler # Problem 87 # Prime power triples import sys sys.path.append('../') from euler import listPrimes import math from bisect import bisect_left cap = 50 * 10**6 a_cap = int(cap**(1 / 2)) primes = listPrimes(int(math.sqrt(cap))) numbers = set() for a in primes: b_cap = bisect_left(primes, (cap - a**2)**(1 / 3)) for b in primes[:b_cap]: c_cap = bisect_left(primes, (cap - a**2 - b**3)**(1 / 4)) for c in primes[:c_cap]: num = a**2 + b**3 + c**4 if num < cap: numbers.add(num) print(len(numbers))
# Project Euler # Problem 37 # Truncatable primes import sys sys.path.append('../') from euler import listPrimes import numpy as np primes = listPrimes(1000000) maxIndex = np.shape(primes)[0] - 1 target = 11 i = 0 count = 0 truncatablePrimes = [] while count < target and i < maxIndex: n = primes[i] i += 1 if n < 10: continue truncatable = True string = str(n) for j in '02468': if j in string[1:]: truncatable = False break if not truncatable: continue
# Project Euler # Problem 49 # Prime permutations import sys sys.path.append('../') from euler import listPrimes searchMax = 10000 primes = listPrimes(searchMax) for i in range(len(primes)-1, -1, -1): if primes[i] <= 1000: primes.pop(i) values = [] for n in primes: deltaMax = (9999 - n) // 2 sortedTarget = sorted(str(n)) for delta in range(2,deltaMax,2): if sorted(str(n+delta)) != sortedTarget: continue elif sorted(str(n+2*delta)) != sortedTarget: continue elif (n + delta) not in primes: continue elif (n + 2*delta) not in primes: continue
# Project Euler # Problem 50 # Consecutive prime sum import sys sys.path.append('../') from euler import listPrimes target = 10**6 longest = 21 primes = listPrimes(target) ans = 2 i = 0 while primes[i] * longest < target: length = longest j = i + longest sumSeq = sum(primes[i:j]) while sumSeq < target: if sumSeq in primes: longest = length ans = sumSeq length += 1 j += 1 sumSeq = sum(primes[i:j]) i += 1