ans = 1 for i in range(1, modulus - n): ans = (ans * i) % modulus ans = gmpy2.invert(ans, modulus) # Since m is an odd-prime, (-1)^(m-n) = -1 if n is even, +1 if n is odd if n % 2 == 0: ans = -1 * ans + modulus return ans % modulus t = time() print "calculating primes up to the 8th power" p = primes(10 ** 8) print "primes generated" summ = 0 p.pop(0) p.pop(0) for x in p: z = x - 1 # factorialMod(x-1,x) z1 = 1 # factorialMod(x-2,x) z2 = z / 2 # factorialMod(x-3,x) z3 = factorialMod(x - 4, x) z4 = factorialMod(x - 5, x) # print x,":",z,z1,z2,z3,z4 z += z1 + z2 + z3 + z4
n += 1 f = defaultdict(list) for p in xrange(2, n): if p not in f: for i in xrange(p + p, n, p): j, k = i, 1 while j % p == 0: j //= p k *= p f[i].append(p) if f[p]==[]:f[p]=[p] return f p = primes(int((10**18/4)**(1/3.))) F = FactorSieve(p[-1]) c = len(p) plist={} for i in xrange(2,200): q = p[i] l = [q] l2 = [] f = F[q-1] for z in f: f1 = F[z-1] for z2 in f1:
# # # Euler 278 # # from time import time from Functions import primes st = time() t = 0 N = 5000 ps = primes(N) C = [] for p in ps: t += p C.append(t) #print C t = 0 for j in xrange(1, len(ps) - 1): q = ps[j] t += (2 * q - 1) * C[j - 1] * (C[-1] - C[j]) - q * ( C[-1] - C[j]) * j - q * (C[j - 1]) * (len(ps) - j - 1) print "sum of f(a,b,c) is ", t print "time elapsed ", time() - st
# # Euler 204 # from Functions import RetFact, IsPrime, primes pns = primes(10**9) summ = 0 for i in xrange(2, 10**9): if i > 100 and not (i in pns): continue zz = sorted(RetFact(i), reverse=True) if zz[0] < 101: print i, zz[0] summ += 1 print print "Hamming Number type 100 <10^9 is", summ + 1
return False return True def cntpowers(x): z = RetFact(x) S = set(z) summ = 0 for SS in S: summ += z.count(SS) return summ N = 4 * 10**5 p = primes(N) X = 5 summm = 0 Z = 60 for i in xrange(Z, Z + 1): if i in p: summm += 1 continue d = divisors(i) D = cntpowers(i) D2 = D / 2 print i, d, D, D2, RetFact(i) for x in d: q = cntpowers(x) if q == D2:
if z/y==(1.0 * z)/y: return False return True def cntpowers(x): z = RetFact(x) S = set(z) summ = 0 for SS in S: summ += z.count(SS) return summ N = 4*10**5 p = primes(N) X=5 summm=0 Z=60 for i in xrange(Z,Z+1): if i in p: summm+=1 continue d = divisors(i) D = cntpowers(i) D2=D/2 print i,d,D,D2,RetFact(i) for x in d: q = cntpowers(x) if q == D2:
# # # Euler 278 # # from time import time from Functions import primes st=time() N = 5000 summ = 0 pr=primes(N) l = len(pr) for i in xrange(l-2): p = pr[i] for j in xrange(i+1,l-1): q = pr[j] pq = p*q peasnqueues = 2*pq - p - q for k in xrange(j+1,l): summ += pr[k]*(peasnqueues) -pq print "sum of f(a,b,c) is ", summ print "time elapsed ", time()-st
# # # Euler 278 # # from time import time from Functions import primes st=time() t=0 N=5000 ps=primes(N) C=[] for p in ps: t+=p C.append(t) #print C t=0 for j in xrange(1,len(ps)-1): q=ps[j] t+=(2*q-1)*C[j-1]*(C[-1]-C[j])-q*(C[-1]-C[j])*j-q*(C[j-1])*(len(ps)-j-1) print "sum of f(a,b,c) is ", t print "time elapsed ", time()-st
from Functions import gcd, RetFact, divisors from math import factorial from Functions import primes from collections import defaultdict from time import time from itertools import combinations st = time() p = primes(10**8) print "time to calculate seive up to 10**8 is ", time()-st print len(p) mx = (10**8)/2 d=defaultdict(int) tot = 1 for i in xrange(2,10**8+1): if i in p: d[i]=1 else: f = RetFact(i) for ff in f: d[ff]+=1 # for dd in d: # print dd,d[dd] # l = sorted(d)
for d in D: tot += d * d return tot st = time() maxp = 44 # res = 15015 # hasRes = [False]* res # for i in xrange(res): # hasRed[i*i % res] = True p = primes(maxp) total = 1 for i in xrange(maxp + 1): #00000): if i in p: continue o = O2(i) if o**.5 == int(o**.5): print i, o, divisors(i), RetFact(i) print total += i print "Sum of O2 for i<64,000,000 is", total print "process time", time() - st
# # # Euler 278 # # from time import time from Functions import primes st = time() N = 5000 summ = 0 pr = primes(N) l = len(pr) for i in xrange(l - 2): p = pr[i] for j in xrange(i + 1, l - 1): q = pr[j] pq = p * q peasnqueues = 2 * pq - p - q for k in xrange(j + 1, l): summ += pr[k] * (peasnqueues) - pq print "sum of f(a,b,c) is ", summ print "time elapsed ", time() - st
s,s0=0,1 t,t0=1,0 r,r0=b,a while r != 0: q = r0/r r0,r = r,r0-q*r s0,s = s,s0-q*s t0,t = t,t0-q*t # print q,r,r0,s,s0,t,t0 # print "bezouts coeffs", s0,t0 # print "gcd ", r0 # print "quots by the gcd", t,s return s0,t0,r0 st=time() p = primes(1000010) l = len(p) summ=0 for i in xrange(2,l-1): j = i+1 v1 = p[i] l1 = len(str(v1)) base = 10**l1 inc = 1 # print v1, base,inc, p[j] delta=p[j]-v1 v2=p[j]
def gm(a, b, c): if g(a, b) > 1: return False #if g(b,c)>1:return False #if g(a,c)>1:return False return True st = time() N = 1000 ctr = 0 print "Calculating Factor Sieve for N=", N, "...", f = FactorSieve(N) print "done." l = [1] p = primes(620) l += p print "Generating list of semi-primes and powers...", for i in xrange(2, N): s0 = set(f[i]) if (len(s0) == 1 and len(f[i]) > 1) or (len(s0) == 2 and len(f[i]) > 2) or (len(s0) == 3 and len(f[i]) > 3): l.append(i) l = sorted(l) print "done." # for x in l: # print x, # exit()
for i in xrange(p + p, n, p): j = i while j % p == 0: j //= p f[i].append(p) #f[i]=[p] #break if p not in f: f[p] = [p] return f st = time() F = FactorSieve(5000000) p = primes(20000000) #print F print time() - st #exit() summ = 0 #x=20000000 #y=5000000 z = 5000000 summ += 4 * 1250000 # extract every 4th numerator with first 1250000 factors of 5000000! 20,000,000/5,000,000 = 4 , etc for x in xrange(19999998, 19900000, -4): # if x in F: # v=sum(F[x])
if g(a,b)==g(b,c)==g(a,c)==1:return True def gm(a,b,c): if g(a,b)>1:return False #if g(b,c)>1:return False #if g(a,c)>1:return False return True st=time() N=1000 ctr=0 print "Calculating Factor Sieve for N=",N,"...", f = FactorSieve(N) print "done." l = [1] p=primes(620) l+=p print "Generating list of semi-primes and powers...", for i in xrange(2,N): s0=set(f[i]) if (len(s0)==1 and len(f[i])>1) or (len(s0)==2 and len(f[i])>2) or (len(s0)==3 and len(f[i])>3):l.append(i) l = sorted(l) print "done." # for x in l: # print x, # exit() for c in l: for a in l: s1=set(f[c]) #if len(s1)>2:continue
# # Euler 204 # from Functions import RetFact,IsPrime,primes pns = primes(10**9) summ = 0 for i in xrange(2,10**9): if i> 100 and not (i in pns):continue zz = sorted(RetFact(i),reverse=True) if zz[0]<101: print i,zz[0] summ+=1 print print "Hamming Number type 100 <10^9 is",summ+1
hasOdds = False for zz in xrange(1,len(psetlist)): if gcd(psetlist[zz],psetlist[zz-1])==1: hasOdds=True break if hasOdds==False:return False return True l=[5,3] N=864 Lim=10**8 #p,q=2,3 p =1217 #,q=2,8191 ctr=0 qp=primes(600000) #F=FactorSieve(2000000) #for q in qp: len(qp)-1000 sctr=0 for qq in xrange(1,len(qp)): q = qp[qq] ii,jj,vv=0,0,0 imax,jmax=0,0 ctr=0 for i in xrange(2,56): #57 for j in xrange(3,36): if gcd(i,j)>1:continue n = [p]*i + [q]*j v = reduce(mul,n)
j = i while j % p == 0: j //= p # f[i].append(p) f[i] = [p] break if p not in f: f[p] = [p] return f st = time() F = FactorSieve(500000) p = primes(20000000) # print F print time() - st # exit() summ = 0 # x=20000000 # y=5000000 z = 5000000 for x in xrange(20000000, 20000000 - 100, -1): if x in p: summ += x print "p", x continue
def A(n): k = 2 while True: r = R(k) z = r / n #print n,k,r,z if n * z == r: return k k += 1 st = time() p = primes(100000) print "time to gen primes", time() - st summ = 0 ctr = 0 for i in xrange(11, 100000, 2): if i in p: continue if gcd(i, 10) > 1: continue v = A(i) w = i - 1 z = w / v if z * v == w: print i ctr += 1 summ += i if ctr == 25: break
def O2(l): tot = 0 D = divisors(n) #print n,D for d in D: tot += d*d return tot st = time() maxp = int(64000000**.5) p=primes(maxp) print "Prime Max", p[-1] total = 0 for i in p: for j in p: z = (i,j) sigma2=O2(z) if int(x**.5)**2 == x: print "n,x:", i*j,i,j,x print "Sum of O2 for i<64,000,000 is",total print "process time",time()-st
if f[p] == []: f[p] = [p] return f, t def phi(n): v = n f = list(set(RetFact(n))) for z in f: v *= (z - 1) v /= z return v st = time() p = primes(40000000) F, Phi = Factor(10000001) print time() - st #print len(Phi) def chain(n, Phi, p): dispch = False if n == 9548417 or n == 9576857 or n == 9577397: dispch = True ctr = 1
q = r0/r r0,r = r,r0-q*r s0,s = s,s0-q*s t0,t = t,t0-q*t # print q,r,r0,s,s0,t,t0 # print "bezouts coeffs", s0,t0 # print "gcd ", r0 # print "quots by the gcd", t,s return r0 print ext_gcd(240,46) exit() p = primes(1000000) l = len(p) summ=0 for i in xrange(2,l-1): j = i+1 v1 = p[i] l1 = len(str(v1)) base = 10**l1 inc = 1 # print v1, base,inc, p[j] while True: ctr=base*inc
if f[p]==[]:f[p]=[p] return f, t def phi(n): v = n f = list(set(RetFact(n))) for z in f: v *= (z-1) v /= z return v st = time() p=primes(40000000) F,Phi=Factor(10000001) print time()-st #print len(Phi) def chain(n,Phi,p): dispch=False if n==9548417 or n==9576857 or n==9577397: dispch=True ctr=1