def totient(n): s = sieve(n) t = float(n) for p in s: if n%p == 0: t *= (1-1./p) return int(t)
def findPrimeCatSet(high, length): final = [] for j in range(1, 3): sieve = pi.sieve(high) mod1 = [3] + [i for i in sieve if i % 3 == j] result = [[i] for i in mod1] leng = length while (leng > 1): temp = [] a = len(result) b = 1 for y in result: print(b, a) b += 1 for x in mod1: if (x > y[-1]): z = y + [x] if (confirmList(z)): temp += [z] result = temp leng -= 1 print(len(result)) final += result print(result) return final
def findPrimeCatSet(high,length): final = [] for j in range(1,3): sieve = pi.sieve(high) mod1 = [3] + [i for i in sieve if i % 3 == j] result = [[i] for i in mod1] leng = length while(leng > 1): temp = [] a = len(result) b = 1 for y in result: print(b,a) b += 1 for x in mod1: if(x > y[-1]): z = y + [x] if(confirmList(z)): temp += [z] result = temp leng -= 1 print(len(result)) final += result print(result) return final
def test_prime_sieve(pot_prime): try: is_prime = pot_prime in FIRST_MILLION_PRIMES assert sieve(pot_prime) is is_prime except TypeError as e: if type(pot_prime) is int and pot_prime >= 0: raise e
def totients(N): L = N+1 t = [i for i in xrange(L)] s = sieve(L) for p in s: for itr in xrange(p,L,p): t[itr] = t[itr]*(1-1./p) return [int(i) for i in t]
def count(n): N = n s = sieve(N) solution = [1] + [0] * N for p in s: for j in xrange(p, N + 1): solution[j] += solution[j - p] return solution[-1]
def solve(): N = 12000 N = int(N) UPPER = N+200 # originally 2*N primes = sieve(int(UPPER)) mins = {k:(1e100,()) for k in range(2,N+1)} for n in range(4,UPPER+1): if n not in primes: for f in gen_factors(n): _k = len(f) s = sum(f) k = n - s + _k if k > N: continue if n <= mins[k][0]: mins[k] = (n,f) return sum(set(i[0] for i in mins.values()))
def totients2(N): L = N+1 s = sieve(L) space = [0]*L space[1] = 1 idx = [i for i in xrange(L)] for p in s: space[p] = p-1 e = 2 while p**e < L: space[p**e] = (p**(e-1))*space[p] e += 1 itr = 2*p while itr < L: space[itr] = (2-p%2)*space[itr/2] itr = itr*2 i = 0 while i < len(s): j = i+1 while j < len(s) and s[i]*s[j] < L: space[s[i]*s[j]] = space[s[i]]*space[s[j]] j += 1 i += 1 for i in xrange(2,L): if space[i] == 0: for p in s: if i%p == 0: d = gcf(p,i/p) space[i] = int(space[p]*space[i/p]*(float(d)/space[d])) if 2*i < L: space[2*i] = (2-i%2)*space[i] e = 2 while i**e < L: if space[i**e] == 0: space[i**e] = (i**(e-1))*space[i] e += 1 break elif 2*i < L: space[2*i] = (2-i%2)*space[i] return space
def recurse(index, result): if (len(result) == listLen): print(result) final.append(result) return result else: for key in range(index, len(keys)): if (numsMatch(result + [keys[key]])): ans = recurse(index + 1 + key, result + [keys[key]]) return None recurse(0, []) return final sieve = pi.sieve(3000) primeSet = set(pi.sieve(3)) mod1 = [3] + [x for x in sieve if x % 3 == 1] dict1 = {} for x in range(len(mod1) - 1): for y in range(x + 1, len(mod1)): if (isPair(mod1[x], mod1[y], primeSet)): dict1[mod1[x]] = dict1.get(mod1[x], []) + [mod1[y]] time1 = time.time() x = connectDict(dict1, 4) time2 = time.time() print(x, time2 - time1) mod2 = [3] + [x for x in sieve if x % 3 == 2]
''' The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. ''' from prime import sieve as sieve primes = sieve(2000000) print sum(primes)
#Solved import prime as pi import permutations as pr import math l = 10000 sieve = pi.sieve(l) sieveSet = set(pi.sieve(100000)) maxP = max(list(sieveSet)) def isPrime(x): if(x < maxP+1): return x in sieveSet elif(x > l ** 8): return pi.isPrime(x) else: index = 0 p = sieve[0] while p**2 <= x: if(x % p == 0): return False index += 1 p = sieve[index] return True def concat(a,b): return a * 10 ** (int(math.log(b,10))+1) + b match_dict = {} def isMatch(a,b): if(a > b): a,b = b,a
#Solved (poorly) import prime import time def isPermutation(x, y): x, y = str(x), str(y) for c in x: if (x.count(c) != y.count(c)): return False return True time1 = time.time() primeSet = set(prime.sieve(10**7)) totDict = {1: 1} minNumber = 0 minValue = 0 for x in range(2, 10**7): if (x not in primeSet): y = prime.lowestFactor(x) if x % (y**2) == 0: tot = totDict[x // y] * y else: tot = totDict[x // y] * (y - 1) totDict[x] = tot if (isPermutation(x, tot)): if (tot / x > minValue): print(x, tot, x / tot) minValue = tot / x minNumber = x
def main(max_n=2 * pow(10, 6) - 1): return sum(prime.sieve(max_n))
def generate_primes(): return filter(lambda k: k > 999, prime.sieve(10000))
"""A composite is a number containing at least two prime factors. For example, 15 = 3 5; 9 = 3 3; 12 = 2 2 3. There are ten composites below thirty containing precisely two, not necessarily distinct, prime factors: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26. How many composite integers, n 108, have precisely two, not necessarily distinct, prime factors?""" import itertools import prime from math import sqrt if __name__ == '__main__': limit = pow(10,8) count = 0 primes = prime.sieve(limit) for i in xrange(len(primes)): for j in xrange(i, len(primes)): if primes[i] * primes[j] >= limit: break count += 1 print count
#Solved import prime sieve = set(prime.sieve(10000)[1:]) squares2 = [2 * x**2 for x in range(1, 100)] def goldbach(z): if (z in sieve): return True for x in squares2: if (x > z): print(x, z) return False elif (z - x in sieve): return True else: print(x, z) return False for x in range(9, 10000, 2): if (not goldbach(x)): print(x) break
#Solved import prime sieve = prime.sieve(100) sieve2 = sieve[0:-1] def product(L): i = 0 prod = 1 while(i < len(L)): prod *= L[i] i += 1 return prod def totRatio(L): totList = [x-1 for x in L] return product(L)/product(totList) l = [sieve2.pop(0) for x in range(5)] print(sieve2[0]) while(product(l) < 100000): l.append(sieve2.pop(0)) print(totRatio(l),product(l))
import prime import math pFlags = prime.sieve(10000000) for comp in range(1,10000000,2): if pFlags[comp]: continue noSum = True for d in range(1,int(math.sqrt(comp/2))+1): dSquare = (d*d)*2 if pFlags[comp-dSquare]: noSum = False break if noSum: print(comp)
#Solved import prime as pi def isPandigital(x): x = str(x) for c in range(1,len(x)+1): if(x.count(str(c)) != 1): return False return True x = pi.sieve(10000000)[::-1] for y in x: if(isPandigital(y)): print(y) break
#Solved import prime as pi import math def exp(a,b,modulus): tempA = a total = 1 while b > 0: log = math.ceil(math.log(modulus,tempA)) total *= tempA ** (b%log) tempA = (tempA ** log) % modulus b //= log return total % modulus sieve = pi.sieve(1000000) print(sieve[0]) y = 0 for x in range(len(sieve)): if(sieve[x] > 10**5): y = x if(x % 2): y -= 1 break log = 2 for x in range(2,len(sieve)): p = sieve[x-1] mod = p**2 a = exp(p-1,x,mod) b = exp(p+1,x,mod) r = (a+b) % mod if(r > 10**10):
#Solved import prime sieve = set(prime.sieve(10000)[1:]) squares2 = [2*x**2 for x in range(1,100)] def goldbach(z): if(z in sieve): return True for x in squares2: if(x > z): print(x,z) return False elif(z-x in sieve): return True else: print(x,z) return False for x in range(9,10000,2): if(not goldbach(x)): print(x) break
import prime as pi import time const = 10**8 def bSearch(G, x): z = const / x lo = 0 hi = len(G) if (G[lo] >= z): return -1 while (lo < hi - 1): mid = (hi + lo) // 2 if (G[mid] >= z): hi = mid else: lo = mid return lo L = pi.sieve(const // 2) total = 0 for i in range(len(L)): z = bSearch(L[i:], L[i]) total += 1 + z if (z < 0): break print(total)
#Solved import prime as pi import permutations as pr sieve = [x for x in pi.sieve(10000) if x > 1000] dict1 = {} def hash1(x): return ''.join(sorted(str(x))) for x in sieve: h = hash1(x) dict1[h] = dict1.get(h,[]) + [x] keys = dict1.keys() triplets = [] for key in keys: x = dict1[key] if(len(x) >= 3): triplets.append(x) for y in triplets: for x in pr.chooseN(y,3): x.sort() if(x[2] - x[1] == x[1] - x[0]): x = [str(a) for a in x] print(''.join(x),"Done") break
from prime import sieve __author__ = 'Winton' #Find the sum of all primes below 2,000,000 print sum(sieve(2000000))
#!/usr/bin/python from prime import sieve primes = sieve(120000) len = 0 for x in primes: len += 1 if len == 10001: print x
#Solved import math import prime def replaceDigits(num,digit): return int(num.replace("x",str(digit))) sieve = set(prime.sieve(5000000)) def testDigitCombination(x): tot = 0 startIndex = 0 if(x.startswith("x")): startIndex = 1 for i in range(startIndex,10): newNum = replaceDigits(x,i) if(newNum in sieve): tot += 1 return tot == 8 def createCombos(num,inserts): digits = math.floor(math.log(num,10)) num = str(num) places = chooseN([x for x in range(digits + inserts)],inserts) newStrs = [] for com in places: newString = "" temp = num[0:-1] for i in range(digits+inserts): if(i in com): newString += "x"
#Solved import prime pList = prime.sieve(1000000) sumList = [0] for x in range(len(pList)): sumList.append(pList[x] + sumList[-1]) sumSet = set(sumList) def sumOfConsecutivePrimes(x): global sumSet global sumList for y in sumList: if (y - x in sumSet): return (x, y) if (y > x): break return False def lengthOfConsecutivePrimes(x, y): global sumList index1 = sumList.index(y) index2 = sumList[0:index1].index(y - x) print(x, index1, index2) return index1 - index2 pList = pList[::-1]
#Solved import prime x = prime.sieve(2000000) print(x) print(sum(x))
#solved import prime as pi import time const = 10**8 def bSearch(G,x): z = const/x; lo = 0 hi = len(G); if(G[lo] >= z): return -1 while(lo < hi-1): mid = (hi+lo)//2 if(G[mid] >= z): hi = mid else: lo = mid return lo L = pi.sieve(const//2) total = 0 for i in range(len(L)): z = bSearch(L[i:],L[i]) total += 1 + z if(z < 0): break print(total)
#Solved (poorly) import prime import time def isPermutation(x,y): x,y = str(x),str(y) for c in x: if(x.count(c) != y.count(c)): return False return True time1 = time.time() primeSet = set(prime.sieve(10**7)) totDict = {1:1} minNumber = 0 minValue = 0 for x in range(2,10**7): if(x not in primeSet): y = prime.lowestFactor(x) if x % (y**2) == 0: tot = totDict[x//y]*y else: tot = totDict[x//y]*(y-1) totDict[x] = tot if(isPermutation(x,tot)): if(tot/x > minValue): print(x,tot,x/tot) minValue = tot/x minNumber = x else: totDict[x] = x-1
10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? ''' import prime import math primes = prime.sieve(100) not_found = True triangle_num =3 counter_num =3 while not_found: factored_num = triangle_num factor_counter = 1 if not prime.miller_rabin(triangle_num): for i in primes: factor_pow=0 while factored_num%i ==0: factor_pow+=1 factored_num=factored_num/i factor_counter*=(factor_pow+1) if factored_num ==1 : break
import datetime from collections import namedtuple from operator import attrgetter import prime # Use a prime sieve to calculate all primes up 31. Then is super easy to check primeness # assuming that no one changes the number of days in a month. PRIMES = prime.sieve(31) class Weather(): """ Weather object that lets you do some basic weather maths. It provides addition and subtraction operations, addition is commutative, subtraction is not. The basic weather types have fixed order so that string representations of combined weather is always the same e.g. Weather('sun') + Weather('rain') == "Sun, Rain" Weather('rain') + Weather('sun') == "Sun, Rain" Note that I think sun is the most important weather type... """ # Use named tuples to create basic weather types with associated emoji and order WeatherType = namedtuple('WeatherType', ['name', 'emoji', 'order']) SUN = WeatherType('Sun', '☀️', 0) RAIN = WeatherType('Rain', '🌧️', 1) WIND = WeatherType('Wind', '💨', 2) CLOUD = WeatherType('Cloud', '☁️', 3) OVERCAST = WeatherType('Overcast', '⛅️', 4)
#Solved import prime pList = prime.sieve(1000000) sumList = [0] for x in range(len(pList)): sumList.append(pList[x]+sumList[-1]) sumSet = set(sumList) def sumOfConsecutivePrimes(x): global sumSet global sumList for y in sumList: if(y-x in sumSet): return(x,y) if(y > x): break return False def lengthOfConsecutivePrimes(x,y): global sumList index1 = sumList.index(y) index2 = sumList[0:index1].index(y-x) print(x,index1,index2) return index1 - index2 pList = pList[::-1] maxP = (0,0) for x in pList: length = sumOfConsecutivePrimes(x)
import prime import math pFlags = prime.sieve(10000000) for comp in range(1, 10000000, 2): if pFlags[comp]: continue noSum = True for d in range(1, int(math.sqrt(comp / 2)) + 1): dSquare = (d * d) * 2 if pFlags[comp - dSquare]: noSum = False break if noSum: print(comp)
import prime N = 1000000 isprime = prime.sieve(N) primesum = 0 sum = 0 cur = 0 max = 0 for v in range(2, 1000): if sum > 1E6: break if isprime[v]: sum += v cur += 1 if isprime[sum]: if cur >= max: primesum = sum max = cur cur = 0 print(primesum)
#Solved import prime as pi def isPandigital(x): x = str(x) for c in range(1, len(x) + 1): if (x.count(str(c)) != 1): return False return True x = pi.sieve(10000000)[::-1] for y in x: if (isPandigital(y)): print(y) break