def IsPractical(n): D = divisors(n) D.remove(n) l = len(D) slist = [1, n] for i in xrange(1, l + 1): C = combinations(D, i) for c in C: s = sum(c) #print i, c, s if s <= n and s not in slist: slist.append(s) #elif s>n+6:break if len(slist) == n: print "*", n return True else: print "##" return False
def IsPractical(n): if n % 2 != 0: return False #D = divisors(n) #if sum(D) == n*2: return True lg = log(n) / log(2) if lg == int(lg): return True F = RetFact(n) S = sorted(list(set(F))) for k in range(1, len(S)): a = S[k] f = S[:k] v = 1 for fn in f: v *= fn**F.count(fn) w = sum(divisors(v)) + 1 if a > w: print a, w, n, S return False print "Yes", n return True
def IsPractical(n): D = divisors(n) D.remove(n) l = len(D) slist = [1,n] for i in xrange(1,l+1): C = combinations(D,i) for c in C: s = sum(c) #print i, c, s if s<=n and s not in slist: slist.append(s) #elif s>n+6:break if len(slist) == n: print "*",n return True else: print "##" return False
def find_chain(n): ap = False n0 = n chain = [n0] strt = n0 while 1: d = divisors(n0) #if n==276:print d nw = sum(d) - n0 #print nw if nw == n and len(chain) == 1: #print "!!!!! Perfect # found" break if nw == n: #print "!!!!! Amicable chain found" ap = True break elif nw == 1: chain.append(1) break elif nw in chain: break elif n0 > 999999: break else: chain.append(nw) n0 = nw return chain, ap
def IsPractical(n): if n%2 != 0: return False #D = divisors(n) #if sum(D) == n*2: return True lg = log(n)/log(2) if lg == int(lg): return True F = RetFact(n) S = sorted(list(set(F))) for k in range(1,len(S)): a = S[k] f = S[:k] v = 1 for fn in f: v*= fn**F.count(fn) w = sum(divisors(v)) + 1 if a > w: print a, w,n,S return False print "Yes",n return True
def find_chain(n): ap = False n0 = n chain = [n0] strt = n0 while 1: d = divisors(n0) #if n==276:print d nw = sum(d) - n0 #print nw if nw == n and len(chain)==1: #print "!!!!! Perfect # found" break if nw == n: #print "!!!!! Amicable chain found" ap = True break elif nw ==1: chain.append(1) break elif nw in chain: break elif n0>999999: break else: chain.append(nw) n0 = nw return chain,ap
def O2(n): tot = 0 D = divisors(n) #print n,D for d in D: tot += d * d return tot
def O2(l): tot = 0 D = divisors(n) #print n,D for d in D: tot += d*d return tot
def f(n): if not IsPrime(1 + n): return False if not IsPrime(2 + n / 2): return False D = divisors(n) D = D[:len(D) / 2] #l = len(D) for d in D: if not IsPrime(d + n / d): return False return True
def f(n): if not IsPrime(1 + n):return False if not IsPrime(2 + n/2):return False D = divisors(n) D = D[:len(D)/2] #l = len(D) for d in D: if not IsPrime(d + n/d):return False return True
def IsSF(d): v = 0 D1 = divisors(d) D1.remove(1) for d1 in D1: if d1**.5 == int(d1**.5): return False return True
def IsPractical(n): D = divisors(n) for k in xrange(2,n): if k in D:continue f = bisect(D,k) if not InD(k,D[:f]): #print n,k return False #print "Yes" return True
def IsPractical(n): D = divisors(n) for k in xrange(2, n): if k in D: continue f = bisect(D, k) if not InD(k, D[:f]): #print n,k return False #print "Yes" return True
def euler283(low=1,top=1000): M=[] st = time() t=0 for m in xrange(low,top+1): print "m:",m, psum = 0 for u in xrange(1,2*m+1): if (2*m)%u: continue for v in xrange(1,int(floor(sqrt(3)*u)+1)): if gcd(v,u)!=1: continue F=4*m**2*(u**2+v**2) #fs=set(RetFact(m)) #fs=fs.union(set(RetFact(u**2+v**2))) #fs.add(2) for d1 in divisors(F): if (d1+2*m*u)%v: continue d2=F/d1 if d1>d2: continue if (d2+2*m*u)%v: continue a=(d1+2*m*u)/v + 2*m*v/u b=(d2+2*m*u)/v + 2*m*v/u c=(d1+d2+4*m*u)/v if b>c: continue #print m,":",a,b,c,a+b+c,d1,d2 t+=a+b+c psum+=a+b+c print psum, print M.append(psum) print t print time()-st print print print M
result = 1 for fs in facts: result *= fs return result #psnum = {} d={} stor=[] for i in range(2,1001): if IsPrime(i):continue v = divisors(i) f0 = RetFact(i) f=f0 v.pop(-1) v.pop(0) for x in v: if x not in f: f += [x] f = sorted(f) #print f k=0 #print "*",len(f),f
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: summm +=1 print "*",x,RetFact(x) #print "*",x,RetFact(x) #if D==X: print print summm print "time elapsed", time()-st
# # # Euler Problem 283 # # from Functions3 import GCD,gcd from math import sqrt from Functions import divisors def ta(a,b,c): A = (((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a))**.5)/4 return A for m in xrange(1,10): f = 8*m**2 fd = divisors(f) print m,f,fd ul = 2*m*2**.5 for d1 in fd: if d1 <=ul: d2 = f/d1 a = d1 + 4*m b = d2 +4 *m c = d1+d2+4*m A = ta(a,b,c) P = a+b+c print " :",d1, d2,":",a,b,c,":",A,P,A/float(P) '''
from Functions import gcd, RetFact, divisors from math import factorial from Functions import primes # p = primes(10**8) # print len(p) # exit() for i in xrange( 7 , 8): n = factorial(i) div = divisors(n) t = 0 sqrtn = int(n**.5) print div for j in div: #if j> sqrtn:break if gcd(j,n/j) == 1: f = RetFact(j) #if set(f)==set([2,3,5,7]): print i, n, len(f),f, j t += j**2 # + (n/j)**2 # print "tot:",i,n,RetFact(t), t%1000000009 # print "tot:",i,RetFact(i),n, t, t%1000000009,RetFact(t%1000000009) # print "tot:",i,n,RetFact(n), t,RetFact(t), t%1000000009 print
# summ+=2 # continue # summ +=sl def canAdd(s,y): #canAdd=True for z in s: if y>z: if y/z==(1.0 * y)/z: return False else: if z/y==(1.0 * z)/y: return False return True d = divisors(120120) x=[] s=set(x) s2=set(x) for i in xrange(1,128): s = set([d[i]]) for j in xrange(i+1,128): #if d[j]%5==0:continue if canAdd(s,d[j]): s=s.union(set([d[j]])) for j in xrange(2,128): #if d[j]%5==0:continue if (d[j] not in s) and canAdd(s,d[j]): s=s.union(set([d[j]]))
# # Euler 380 # # from Functions import RetFact,divisors ctr = 0 for b in xrange(1,333334): ctr+=1 b2=b**2 f = divisors(b2) #f.remove(1) f.remove(b2) l = len(f) for i in xrange(l-1,1,-1): c=f[i] a = b2/c if a<=b<=c and a+c>b and max(a,c)-min(a,c)<b: print b, a, c ctr+=1 else: continue print ctr
def ta(a, b, c): A = (((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a)) ** 0.5) / 4 return A summ = 0 # # Using Heron's formula from Lubomir Markov's extension of Goehl's Method # M = [] for m in xrange(1, 1001): print "m:", m, psum = 0 # u = 2*m fd0 = divisors(2 * m) # Factoring u = 2m for u in fd0: ul = u * 3 ** 0.5 # choosing factors up to u*sqrt(3) mxul = int(ul) + 1 for v in xrange(1, mxul): if gcd(u, v) != 1: continue # choosing v coprime to u f = 4 * m * m * (u * u + v * v) # = delta1 * delta2 fd = divisors(f) # obtaining factors for d1,d2 ul2 = 2 * m * ((u * u + v * v) ** 0.5) # obtaining sqrt to avoid repetitions # print "!",m,u,v # showing m,u,v for curiosity's sake for d1 in fd: if ((2 * m) * (v ** 2 - u ** 2) / u) <= d1 <= ul2: # choosing range of delta1 to ensure A=mP d2 = f / d1 a = (d1 + 2 * m * u) / v + (2 * m * v) / u b = (d2 + 2 * m * u) / v + (2 * m * v) / u
# # Euler Problem 359 # # from Functions import RetFact, divisors facts = RetFact(71328803586048) rooms = divisors(71328803586048) print len(rooms) # 1 1,1 # 2 2,1 # 3 1,2 = 4 # 4 3,1 # 5 4,1 # 6 3,2 = 9 # 7 2,2 = 9 # 8 1,3 = 9 # 9 5,1 # 10 6,1 # 11 5,2 = 16 # 12 4,2 = 16 # 13 3,3 = 16 # 14 2,3 = 16 # 15 1,4 = 16 # 16 7,1 # 17 8,1
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 Problem 283 # # from Functions3 import GCD, gcd from math import sqrt from Functions import divisors def ta(a, b, c): A = (((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a))**.5) / 4 return A for m in xrange(1, 10): f = 8 * m**2 fd = divisors(f) print m, f, fd ul = 2 * m * 2**.5 for d1 in fd: if d1 <= ul: d2 = f / d1 a = d1 + 4 * m b = d2 + 4 * m c = d1 + d2 + 4 * m A = ta(a, b, c) P = a + b + c print " :", d1, d2, ":", a, b, c, ":", A, P, A / float(P) ''' summ = 0 ctr,ctr1 = 0,0 for m in xrange(2, 4000):
# summ+=2 # continue # summ +=sl def canAdd(s,y): #canAdd=True for z in s: if y>z: if y/z==(1.0 * y)/z: return False else: if z/y==(1.0 * z)/y: return False return True d = divisors(120120) x=[] s=set(x) s2=set(x) for x in d: z = RetFact(x) S = set(z) summ = 0 for SS in S: summ += z.count(SS) if summ == 4: print x
from Functions import RetFact, divisors,IsPrime summ = 6 for n in xrange(3, 6): if IsPrime(n): print n,n%4 if n%4==1 : pass else: summ += (n+1) else: d = divisors(n) print n,d summ +=sum(d) print print summ # # 6 20 1 ,6, 3, # 7 8 1 ,7 # 8 29 # 9 13 # 10 56 # 11 12 # 17
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
from Functions import RetFact, divisors, IsPrime summ = 6 for n in xrange(3, 6): if IsPrime(n): print n, n % 4 if n % 4 == 1: pass else: summ += (n + 1) else: d = divisors(n) print n, d summ += sum(d) print print summ # # 6 20 1 ,6, 3, # 7 8 1 ,7 # 8 29 # 9 13 # 10 56 # 11 12 # 17
from Functions import gcd, RetFact, divisors from math import factorial from Functions import primes # p = primes(10**8) # print len(p) # exit() for i in xrange(7, 8): n = factorial(i) div = divisors(n) t = 0 sqrtn = int(n**.5) print div for j in div: #if j> sqrtn:break if gcd(j, n / j) == 1: f = RetFact(j) #if set(f)==set([2,3,5,7]): print i, n, len(f), f, j t += j**2 # + (n/j)**2 # print "tot:",i,n,RetFact(t), t%1000000009 # print "tot:",i,RetFact(i),n, t, t%1000000009,RetFact(t%1000000009) # print "tot:",i,n,RetFact(n), t,RetFact(t), t%1000000009 print
return A summ = 0 # # Using Heron's formula from Lubomir Markov's extension of Goehl's Method # M = [] from time import time st = time() for m in xrange(1, 1001): #print "m:",m, psum = 0 #u = 2*m fd0 = divisors(2 * m) # Factoring u = 2m for u in fd0: ul = u * 3**.5 # choosing factors up to u*sqrt(3) mxul = int(ul) + 1 for v in xrange(1, mxul): if gcd(u, v) != 1: continue # choosing v coprime to u f = 4 * m * m * (u * u + v * v) # = delta1 * delta2 fd = divisors(f) # obtaining factors for d1,d2 ul2 = int( 2 * m * ((u * u + v * v)**.5)) # obtaining sqrt to avoid repetitions #print "!",m,u,v # showing m,u,v for curiosity's sake for d1 in fd: if ((2 * m) * (v**2 - u**2) / u ) <= d1 <= ul2: #choosing range of delta1 to ensure A=mP if (d1 + 2 * m * u) % v: continue
from Functions import divisors,IsPrime def IsSF(d): v = 0 D1 = divisors(d) D1.remove(1) for d1 in D1: if d1**.5 == int(d1**.5): return False return True summ = 0 for k in xrange(2,101): cnt = 0 D = divisors(k) D.remove(1) for d in D: if IsSF(d): cnt +=1 print "!",k,d,cnt summ += cnt print k, cnt print summ