def factor3(N): N += 2 eqn_sieve = [k * k for k in xrange(N)] print eqn_sieve print eqfactlist = [()] * N print "!!!", type(eqfactlist[2]), eqfactlist[2] primes = RofPrimes(2, N + 1) for k in xrange(2, N): num = eqn_sieve[k] if num > 1: # number still needs factoring if IsPrime(k): j = k else: continue #j = primes[k] while j < N: while eqn_sieve[j] % j == 0: eqn_sieve[j] /= j z = eqfactlist[j] z += eval(str(j) + ",") eqfactlist[j] = z j += j return eqfactlist
def prob51(): primes = RofPrimes(10000, 1000000) # test primes in list of primes under 10Meg for p in primes: if p < 10000: continue s = str(p) for c in '012': if s.count(c) not in (3, 6, 9): continue losers = [] winners = [] for d in '0123456789': n = int(s.replace(c, d)) # see if candidate family member is prime if d >= c and n == primes[bisect(primes, n) - 1]: winners.append(n) else: losers.append(n) # if more than 2 losers, then can't be a # family of eight primes if len(losers) > 2: break return winners
def p342(max_n=10**10): res = 0 max_n -= 1 # max_n can be reached. p_list = RofPrimes(2, int(sqrt(max_n))) #primes(sqrt(max_n)) #print p_list #p_list="" #exit() for i, p in enumerate(p_list): print "!", i, p pp = p * p ppp = pp * p p6 = ppp * ppp max_n_new = max_n / pp phi = ppp * (p - 1) mul = pp while max_n_new > 0: res += mul * BT(p_list, max_n_new, i, phi) max_n_new /= ppp phi *= p6 mul *= ppp return res
from Functions import RofPrimes, RetFact,IsPrime,gcd x = RofPrimes(2,200000) x.append(1) print "init sum", sum(x) r = list(x) x = sorted(x, reverse = True) xs = RofPrimes(2,444) xs = sorted(xs,reverse=False) ''' xs.remove(421) xs.remove(443) xs.remove(349) xs.remove(379) xs.remove(241) xs.remove(83) xs.append(421) xs.append(443) xs.append(349) xs.append(379) xs.append(241) xs.append(83) x.remove(3) xs.remove(3) x.append(177147)
if val<=MX: diff = val - p - x if diff>maxdiff: #print p,b,n,x,val,diff maxval,bsv,xsv,maxdiff = val,b,x,diff else: break return maxval,bsv,xsv,maxdiff ################### st = time() MX = 1000000 SubSet = RofPrimes(2,MX) # Add 1 SubSet.append(1) print "init sum", sum(SubSet) SqMax = int(MX**.5)+1 Srch = RofPrimes(SqMax-1,MX) SqSet = sorted(RofPrimes(2,SqMax),reverse = False) pused=[] dupes = [] ctr = 0 prev = 0
from Functions import IsPrime, RofPrimes from itertools import combinations Spot = 10**3 #P=[] P = RofPrimes(2, 100) print P print len(P) X = combinations(P, 4) for x in X: print x
# # # # x + y = xy/n # # 1/x = 1/n - 1/y # # # from Functions import RetFact, IsPrime,RofPrimes n = 2768774904222066200260800 # 720720000/(72*5) #500000000 maxx = 0 lim = 4000000 x=RofPrimes(17,1000) ctr = 1 while 1: #n*=x[ctr] #if IsPrime(n): # n+=1 # continue x = RetFact(n) s = set(x) facts = [] for f in s: facts.append(x.count(f)*2+1) m=1 for f in facts:
chk1, chk2 = False, False #print "Q",int(str(p1)+str(p2)),IsPrime(int(str(p1)+str(p2))) #print "T",int(str(p2)+str(p1)),IsPrime(int(str(p1)+str(p2))) #print "!",len(primes) if not IsPrime(int(str(p1) + str(p2))): return False if not IsPrime(int(str(p2) + str(p1))): return False return True ################################## [j] st = time() primes = RofPrimes(3, 20001) #x=RofPrimes(3,100001000) for p1 in RofPrimes(3, 10000): #if p1==13:print "GRRR" for p2 in RofPrimes(p1, 10000): #if p2 == 5197: print "GRRRRRRRRR!" if ConcPrimes(p1, p2): if p1 == 13 and p2 == 5197: print "RAWR!" for p3 in RofPrimes(p2, 10000): if ConcPrimes(p1, p3) and ConcPrimes(p2, p3): #print "*",p1,p2,p3 for p4 in RofPrimes(p3, 10000): if ConcPrimes(p1, p4) and ConcPrimes( p2, p4) and ConcPrimes(p3, p4): #print "**",p1,p2,p3,p4
# # # Euler 132 # # from Functions import RofPrimes from time import time st = time() P = RofPrimes(11, 10**6) ctr = 0 tot = 0 for p in P: if pow(10, 10**9, p) == 1: print p, tot += p ctr += 1 if ctr == 40: break print print print "Sum of first 40 primes in R(10**9) is", tot print "process time is", time() - st
from Functions3 import GCD,gcd from itertools import product from Functions import RofPrimes,RetFact def notcoprime(cp,Y): for yy in Y: if gcd(yy,cp)!=1: return yy return -1 ##################### MX=200000 x = RofPrimes(2,MX) x2 = RofPrimes(2,444) #x.append(1) T=[] for i in xrange(len(x2)): tmp=[] p=0 while 1: p+=1 v0=x[i] v = x[i]**p if v>MX or v0>=MX:break #print "!",i,p,x[i],v tmp.append(v)
def invmod1(a,p): r = a d = 1 for count in xrange(p): d = ((p //r + 1) * d) %p r = (d*a) % p if r ==1: break return d ################################################# st = time() primes = RofPrimes(1000,5000) pdict = {} tot = 0 for P in primes: z = Lucas(10**18, 10**9, P) pdict[P] = z #print P,z L = len(primes) for i in xrange(L-2): for j in xrange(i+1,L-1): for k in xrange(j+1,L): # use chinese remainder theorem
from itertools import combinations from Functions import RofPrimes,RetFact,IsPrime def Sum(L): return sum(L) def notcoprime(cp,Y): for yy in Y: if gcd(yy,cp)!=1: return yy return -1 MX=10 x= RofPrimes(2,MX) print GCD(x) print x print Sum(x) y = list(x) #x = sorted(x,reverse = False) for i in xrange(MX-1,0,-1): if i in y:continue z= notcoprime(i,x) print i,z if z > 0 and i>z: x.remove(z) else: x.append(i)
# Euler 358 # # from Functions import RofPrimes def HasAll(n): nstr = str(n) for j in xrange(1, 10): if str(j) not in nstr: return False return True b = 10 p = 7 #print (b**(p-1) -1)/p M = 7 P = RofPrimes(M, M + 100) for p in P: print #print p, (pow(b,p-1)-1)/p # (b**(p-1) -1)/p c = (b**(p - 1) - 1) / p l = len(str(c)) #if c%100000==56789: #if HasAll(c): print p, c, l, len(str(c * l)), len(str(c * (l + 1))) # break
# # from Functions import RofPrimes def HasAll(n): nstr=str(n) for j in xrange(1,10): if str(j) not in nstr:return False return True b = 10 p = 13 P = RofPrimes(7,100) for p in P: t = 0 r = 1 n=0 while 1: #print "t", t t += 1 x = r*b d = int(x/p) r = x%p n = n*b + d if r != 1: continue
def ConcPrimes(p1,p2): if (int(str(p1)+str(p2)) not in primes) or (int(str(p2)+str(p1)) not in primes):return False return True ################################## [j] st = time() primes=RofPrimes(3,100000) x=RofPrimes(3,10000) #a=permutations(x,2) a=combinations(x,2) y=[] sy=[] for z in a: #print "!",z if ConcPrimes(z[0],z[1]): y.append(z) sy.append(z[0]) sy.append(z[1]) #print z print y
f = list(set(RetFact(n))) num, div = 1, 1 for x in f: num *= x - 1 div *= x return n * num / div def chain(m): ctr = 1 while 1: ctr += 1 m = phi(m) if m == 1: return ctr x = RofPrimes(2, 40000000) summ = 0 for i in xrange(len(x) + 1): n = x[i - 1] l = chain(n) print n, l if l == 25: #print n, l summ += n print "answer is", summ
def Sum(L): return sum(L) def notcoprime(cp,Y): for yy in Y: if gcd(yy,cp)!=1: return yy return -1 from math import log print notcoprime(6, [5, 7, 9, 8]),"!" MX=20 x= RofPrimes(2,MX) y=list(x) tmp=[] tmpr=[] for y1 in x: z = log(MX)/log(y1) z1 = y1**int(z) tmp.append(z1) tmpr.append(y1) print x print tmp for z in tmp: x.append(z)
from Functions import RetFact, IsPrime, RofPrimes def mobius(n): result, i = 1, 2 while n >= i: if n % i == 0: n = n / i if n % i == 0: return 0 result = -result i = i + 1 return result mx = 33554432 primes = RofPrimes(2, mx) lim = 2**50 ctr = 0 print len(primes) print for i in primes: fact = i**2 ctr += int(lim / fact) * mobius(i) print ctr
for i in range(0,len(nstr)): s[int(nstr[i])] += 1 #print s for j in range(0,len(s)): if s[j]>m-1: retval += (j+1)*s[j] #print retval return retval x=RofPrimes(10000,200000) print x print print "________________" for i in range(0,len(x)): m=3 if findMults(x[i],m)>m-1: temp =str(x[i]) print temp[4:] if temp[5:6]=='33' : print "hit!",x[i]
if diff > maxdiff: #print p,b,n,x,val,diff maxval, bsv, xsv, maxdiff = val, b, x, diff else: break return maxval, bsv, xsv, maxdiff ################### st = time() MX = 1000000 SubSet = RofPrimes(2, MX) # Add 1 SubSet.append(1) print "init sum", sum(SubSet) SqMax = int(MX**.5) + 1 Srch = RofPrimes(SqMax - 1, MX) SqSet = sorted(RofPrimes(2, SqMax), reverse=False) pused = [] dupes = [] ctr = 0 prev = 0
# # # Euler Problem 187 # # from Functions import RofPrimes from time import time from bisect import bisect from math import sqrt st = time() PRIMES = RofPrimes(2, 10**8 / 2) N = 10**8 TOTAL = 0 for x in range(bisect(PRIMES, sqrt(N))): p = PRIMES[x] z = bisect(PRIMES, N / p) - x print x, p, N / p, z, PRIMES[z - 1] TOTAL += z print TOTAL print "process time is", time() - st
# # Euler 302 # # [] from Functions import RofPrimes, RetFact, IsPrime from itertools import combinations maxp = 11 p = RofPrimes(2, 11) lim = 10**4 def tf(f): t = list(f) for f1 in f: zz = RetFact(f1 - 1) #print "zz",zz for zzz in zz: #print "zzz",zzz if zzz not in f: t.append(zzz) t = sorted(list(set(t))) return t for x in combinations(p, 2): sp = tf(x)
from Functions import RofPrimes, RetFact, IsPrime, gcd from itertools import permutations, combinations pm = [421, 443, 349, 379, 241, 83] pms = permutations(pm) for xms in pms: print xms, "######" x = RofPrimes(2, 200000) x.append(1) r = list(x) x = sorted(x, reverse=True) xs = RofPrimes(2, 444) xs = sorted(xs, reverse=False) for zz in xms: x.remove(zz) for zz in xms: x.append(zz) # x = sorted(x, reverse = False) ps = [] ps2 = [] l = [] rj = [] exc = range(2, 20) for sp in xs:
# # # Problem 347 # # from math import log10 from Functions import RofPrimes from time import time st = time() N = 10**7 p = RofPrimes(2, N / 2 + 1) # get range of primes #print "primes",p logN = log10(N) tot = 0 # search through range of prime pairs p[i],p[j] for i in xrange(0, len(p) + 1): isFound = False # detect when loop is churning out nothing but zeros for j in xrange(i + 1, len(p)): p1, p2 = p[i], p[ j] # pull to save time for searching list multiple times m1 = int(logN / log10(p1)) # find the highest possible exponent m2 = int(logN / log10(p2)) mn = 0 # set max # found to zero
s[int(nstr[i])] += 1 #print s for j in range(0, len(s)): if s[j] > m - 1: retval += (j + 1) * s[j] #print retval return retval ################################# x = RofPrimes(10000, 1000000) #print x print "number of primes searched:", len(x) print print "________________" y = [] z = [] ctr = 0 for i in range(0, len(x)): m = 2 if findMults(x[i], m) > m - 1: temp = str(x[i])
import operator from Functions import RofPrimes primes = RofPrimes(2,100001) #@memoize def prime_factors(n): if n <=1: return [] for p in primes: if n%p==0: break while (n%p)==0: n/=p return [p] + prime_factors(n) def rad(n): return reduce(operator.mul, prime_factors(n),1) print sorted(range(1,100001), key=rad)[10000-1]
# # # Euler Problem 187 # # from Functions import IsPrime, RetFact, RofPrimes from time import time st = time() primes1 = RofPrimes(2, (10**8 / 2)) primes2 = RofPrimes(2, int((10**8)**.5) + 1) ctr = 0 print "!!", len(primes1), len(primes2) for a in primes2: for b in primes1: if b >= a: if a * b < 10**8: ctr += 1 else: break #ctr = len(primes1) * len(primes2) print print "total is", ctr print "process time is", time() - st
from Functions import RofPrimes, RetFact, IsPrime, gcd from itertools import permutations, combinations pm = [421, 443, 349, 379, 241, 83] pms = permutations(pm) for xms in pms: print xms, "######" x = RofPrimes(2, 200000) x.append(1) r = list(x) x = sorted(x, reverse=True) xs = RofPrimes(2, 444) xs = sorted(xs, reverse=False) for zz in xms: x.remove(zz) for zz in xms: x.append(zz) #x = sorted(x, reverse = False) ps = [] ps2 = [] l = [] rj = [] exc = range(2, 20) for sp in xs:
# # # Euler Problem 87 # summ = 0 from Functions import RofPrimes from time import time st = time() r1 = RofPrimes(2, 7100) r2 = RofPrimes(2, 380) r3 = RofPrimes(2, 92) numlist = [] for i in r1: a = i**2 for j in r2: b = j**3 for k in r3: bignum = a + b + k**4 if bignum < 50000000: #print i,j,k,bignum numlist.append(bignum) numset = set(numlist) summ = len(numset) print "Answer is", summ print "Process time is", time() - st
l = len(ns) a = ns[:l-1] b = ns[-1] #print a, b x = int(a) + int(b)*m return x ''' print 4407/113. x=f(4407,34) print x,x/113. ''' st = time() summ = 0 primes = RofPrimes(2,10**3) for p in primes: for i in xrange(p,0,-1): n = 9*p x = f(n,i) n1,x1=n/float(p), x/float(p) if isInt(n1) and isInt(x1): #print p, i summ += i break print print "total is", summ print "process time is",time()-st
from Functions import RofPrimes, RetFact,IsPrime,gcd x = RofPrimes(2,200000) x.append(1) r = list(x) x = sorted(x, reverse = True) xs = RofPrimes(2,444) xs = sorted(xs,reverse=False) x.remove(421) x.remove(443) x.remove(349) x.remove(379) x.remove(241) x.remove(83) x.append(421) x.append(443) x.append(349) x.append(379) x.append(241) x.append(83) ''' x.remove(3) xs.remove(3) x.append(177147) ''' #x = sorted(x, reverse = False)
if k==0 and l1==0:continue n1=int(s[:k]+str(l1)+s[k+1:]) if IsPrime(n1): #print "hit!",j,i,":",n,n1 return False ''' return True ##################### st=time() squbes=[] limit = 2.5 * 10**11 substr="200" ctr=0 for j in RofPrimes(2,7) : #in RofPrimes(1,2): for i in RofPrimes(2,200000): #if i<j:continue x = j**3 * i**2 x2 = j**2 * i**3 if x<limit and (substr in str(x) and primeproof2(x,j,i)): ctr+=1 print ctr,j,i,":", x squbes.append(x) if x2<limit and (substr in str(x2) and primeproof2(x2,j,i)): ctr+=1 print ctr,j,i,"::", x2 squbes.append(x2)
else: return mid return lo def ml(L): v = 1 for lv in L: v *= lv return v st = time() primes = RofPrimes(2, 190) p = 1 for P in primes: p *= P b = int(p**.5) #print "b:",b s1, s2, = [], [] for i in xrange(0, 42): if i % 2 == 0: s1.append(primes[i]) else: s2.append(primes[i])
return ctr if m == 1: chaindict[m0] = ctr #print "chain",m0,ctr return ctr + 1 else: ctr += 1 ######################### st = time() MX = 10000 #40000000 x = RofPrimes(2, MX) totient = Totient(MX) Phi = [] for phi in imap(totient, range(10000)): Phi.append(phi) summ = 0 #for a,b in enumerate(Phi): # print a,b print "__________" chaindict = {1: 1}
def ConcPrimes(p1, p2, primes): chk1, chk2 = False, False #print "Q",int(str(p1)+str(p2)),IsPrime(int(str(p1)+str(p2))) #print "T",int(str(p2)+str(p1)),IsPrime(int(str(p1)+str(p2))) if not (int(str(p1) + str(p2)) in primes): return False if not (int(str(p2) + str(p1)) in primes): return False return True ################################## [j] xw = RofPrimes(3, 100000) x = RofPrimes(3, 10000) #print x1 #exit() ''' x=[] for x0 in x1: zzz = str(x0) #print zzz,type(zzz) if int(zzz[::-1]) %2 != 0: x.append(x0) ''' print len(x) #a=permutations(x,2)
def notcoprime(cp, Y): for yy in Y: if gcd(yy, cp) != 1: return yy return -1 ################# st = time() #for MX in range(200000, 200001): for MX in range(30, 31): x = RofPrimes(2, MX) y = list(x) tmp = [] tmpr = [] for y1 in x: z = log(MX) / log(y1) z1 = y1**int(z) #print y1, z1 tmp.append(z1) tmpr.append(y1) #exit() for z in tmp: x.append(z) for z in tmpr:
def notcoprime(cp,Y): for yy in Y: if gcd(yy,cp)!=1: return yy return -1 ################# st=time() for MX in range(200000, 200001): #for MX in range(10, 101): x= RofPrimes(2,MX) y=list(x) tmp=[] tmpr=[] for y1 in x: z = log(MX)/log(y1) z1 = y1**int(z) tmp.append(z1) tmpr.append(y1) for z in tmp: x.append(z) for z in tmpr: x.remove(z) x = sorted(x)