def solve(m, m2): tStart = time.time() ways = [[0 for _ in range(m2+2)] for _ in range(m2+2)] maxPrime = 10000 pr = set(help.primesUpTo(maxPrime)) n = 1 while getWays(n, n, pr, ways) < m and n < m2: n += 1 if n >= maxPrime: maxPrime *= 10 pr = set(help.primesUpTo(maxPrime)) print(n) print("Run Time = " + str(time.time() - tStart))
def solve(max): tStart = time.time() max2 = math.ceil(math.sqrt(max)) max3 = math.ceil(math.pow(max,1/3)) max4 = math.ceil(math.pow(max,1/4)) pr2 = help.primesUpTo(max2) pr3 = help.primesUpTo(max3) pr4 = help.primesUpTo(max4) numbers = [] for p2 in pr2: for p3 in pr3: for p4 in pr4: prod = p2**2 + p3**3 + p4**4 if prod < max: numbers.append(prod) print(len(set(numbers))) print("Run Time = " + str(time.time() - tStart))
def solve(max, maxPrime): tStart = time.time() pr = help.primesUpTo(maxPrime) for i in range(0, len(pr)): n = i + 1 if f(pr[i]) > max: print(n) print("Run Time = " + str(time.time() - tStart)) return
def solve(n,m): tStart = time.time() pr = help.primesUpTo(m+1) l = [] for i in range(1,m+1): l.append([i,rad(i,pr)]) print(sorted(l,key=lambda h: h[1])[n-1]) print("Run Time = " + str(time.time() - tStart))
def solve(max,max2): tStart = time.time() pri = help.primesUpTo(max) pr = set(help.primesUpTo(max2)) min = 0 for p1 in pri: for p2 in pri: if p2 > p1 and hasP(p1,p2,pr): for p3 in pri: if p3 > p2 and hasP(p1,p3,pr) and hasP(p2,p3,pr): for p4 in pri: if p4 > p3 and hasP(p1,p4,pr) and hasP(p2,p4,pr) and hasP(p3,p4,pr): for p5 in pri: if p5 > p4 and hasP(p1,p5,pr) and hasP(p2,p5,pr) and hasP(p3,p5,pr) and hasP(p4,p5,pr): s = p1+p2+p3+p4+p5 if s < min or min == 0: min = s print(min) print("Run Time = " + str(time.time() - tStart))
def solve(max): tStart = time.time() pr = help.primesUpTo(max) lastCount = 2 n = 0 for i in range(3, max): count = countDiv(i, pr) if count == lastCount: n += 1 lastCount = count print(n) print("Run Time = " + str(time.time() - tStart))
def solve(max): tStart = time.time() primes = help.primesUpTo(math.ceil(math.sqrt(max))) mintotient = 1 minratio = 1000 for i in range(2, max): totient = help.totient(i, primes) if isPerm(i, totient) and minratio > i/totient: mintotient = totient minratio = i/totient print(i, mintotient, minratio) print("Run Time = " + str(time.time() - tStart))
def solve(m): last = [1,1] all = set([1]) for j in range(1, m): current = [1] for i in range(1,len(last)): n = last[i]+last[i-1] all.add(n) current.append(n) current.append(1) last = current pr = help.primesUpTo(50) print(sum(list(filter(lambda x: isSquareFree(x, pr), all))))
def solve(start, max, plus, min): tStart = time.time() d = start minres = 1 primes = [] primes = help.primesUpTo(math.ceil(math.sqrt(max))) while d <= max: d += plus res = resilience(d, primes) if minres > res: minres = res print(minres, d) if minres < min: print(d) break print("Run Time = " + str(time.time() - tStart))
def solve(max): tStart = time.time() pr = help.primesUpTo(int(max/2)) count = 0 for i in range(0, len(pr)): for j in range(i, len(pr)): if pr[i]*pr[j] < max: count += 1 else: break print(count) print("Run Time = " + str(time.time() - tStart))
def solve(max): tStart = time.time() pr = help.primesUpTo(math.ceil(math.sqrt(max))) print(sum(list(map(lambda n: help.totient(n, pr), range(2,max+1))))) print("Run Time = " + str(time.time() - tStart))
#Totient Chains #Problem 214 import time import help maxprime = 40000000 pr = help.primesUpTo(maxprime) solved = dict() solved[1] = 1 def getTot(p): if p not in solved: solved[p] = 1 + getTot(help.totient(p, pr)) return solved[p] tStart = time.time() mysum = 0 for p in pr: if getTot(p) == 25: print(p) mysum += p print(mysum) print("Run Time = " + str(time.time() - tStart))
# Prime pair connection #Problem 134 import time import help def getFactors(p1, p2, d): for i in range(0, 10): if ((p1 * i + d) % 10) == (p2 % 10): if p2 // 10 == 0: return i else: return i + 10 * getFactors(p1, p2 // 10, (p1 * i + d ) // 10) def getS(p1, p2): F = getFactors(p2, p1, 0) return F * p2 maxnumber = 1000000+4 pr = help.primesUpTo(maxnumber) tStart = time.time() mysum = 0 for i in range(2,len(pr)-1): mysum += getS(pr[i], pr[i+1]) print(mysum) print("Run Time = " + str(time.time() - tStart))
def solve(max): pr = help.primesUpTo(max) print(sum(list(map(S, pr))))
#Pandigital prime sets #Problem 118 import time import help maxprime = 98765432 pr = set(help.primesUpTo(maxprime)) def getNumbers(S): for n in sorted(map(int, list(S))): yield n for k in getNumbers(strDiff(S, str(n))): yield k*10+n def strDiff(S, T): s = '' for token in S: if not token in T: s += token return s def isPrime(n): if n < maxprime: return n in pr else: return help.isPrime(n) def run(): tStart = time.time() S1 = '123456789'
# Problem 127 import unittest from fractions import gcd import help g_upperBound = 120000 g_primes = help.primesUpTo(g_upperBound) g_primesSet = set(g_primes) g_solvedRad = [] for i in range(g_upperBound): g_solvedRad.append([i,1]) for i in range(2, g_upperBound): if g_solvedRad[i][1] == 1: g_solvedRad[i][1] = i for j in range(2*i, g_upperBound, i): g_solvedRad[j][1] *= i; g_sortedRad = sorted(g_solvedRad,key=lambda h: h[1]) def isABCHit(a, b, c): return gcd(a,b) == 1 and a < b and a + b == c and rad(a)*rad(b)*rad(c) < c def isABCHitSimple(a, b, c): return rad(a)*rad(b)*rad(c) < c and gcd(a,b) == 1
#Problem 131 import time import help tStart = time.time() m = 6 pr = set(help.primesUpTo(10**m)) c = 0 d = 0 n = 1 while d < 10**m: d = (n+1)**3-n**3 if d in pr: print(d) c += 1 n += 1 print(c) print("Run Time = " + str(time.time() - tStart))
#Prime connection #Problem 425 import time import help from math import log from heapq import heappush, heappop m = 6 connectedDict = dict() sortedPr = help.primesUpTo(10**m) pr = set(sortedPr) def getConnected(p): if p not in connectedDict: connected = [] for i in range(10): connected.append(p + i*10**len(str(p))) for i in range(len(str(p))): for r in replacements(p, i): if not r == 0 and int(log(r,10)) == int(log(p,10)): connected.append(r) connected = list(filter(lambda x: x in pr and not x == p, connected)) connectedDict[p] = sorted(connected) return connectedDict[p] def replacements(p, i): for j in range(10): yield replace(p, i, j) def discardLeft(p):
def solve(m, max): pr = help.primesUpTo(m) print(countHamming(pr, max))
def blah(l): pr = help.primesUpTo(100) if count(l) >= 4000000: print(calcNumber(l, pr))
def getPrimeSumOfBinom(n, m): mysum = 0 pr = help.primesUpTo(n) for p in pr: mysum += p * countExpInBinom(n, m, p) return mysum
# Prime pair connection #Problem 134 import time import help def getRep(n): return ( 10**n -1 ) // 9 tStart = time.time() m = 5 #5 k = 100 primes = help.primesUpTo(10**m) mySum = 0 for p in primes: if not 1 == pow(10,10**k, 9 * p): #print(p) mySum += p print(mySum) print("Run Time = " + str(time.time() - tStart))