def p141(): L = [] s, limita, limitm = 0, 10**4, 10**12 for a in xrange(2, limita): for b in xrange(1, a): if (a**3) * b + (b**2) >= limitm: break if gcd(a, b) > 1: continue c = 1 while True: m2 = (c**2) * (a**3) * b + c * (b**2) if m2 > limitm: break else: if m2**.5 == int(m2**.5): s += m2 L.append(m2) c += 1 print 'Solution: {}'.format(s) L = sorted(L) for m2 in L: print m2, RetFact(m2)
def p141(): L = [] s, limita, limitm = 0, 10**4, 10**12 for a in xrange(2,limita): for b in xrange(1,a): if (a**3)*b+(b**2)>=limitm: break if gcd(a,b)>1: continue c=1 while True: m2=(c**2)*(a**3)*b+c*(b**2) if m2>limitm: break else: if m2**.5==int(m2**.5): s+=m2 L.append(m2) c+=1 print 'Solution: {}'.format(s) L=sorted(L) for m2 in L: print m2, RetFact(m2)
def gcd2(l): bzzz = True for z in xrange(len(l)): for y in xrange(len(l)): if z==y:continue if gcd(l[z],l[y])>1: bzzz = False break return bzzz
def gcd2(l): bzzz = True for z in xrange(len(l)): for y in xrange(len(l)): if z == y: continue if gcd(l[z], l[y]) > 1: bzzz = False break return bzzz
def R(M): tot = 0.0 c = int(ceil(M/2)) for i in xrange(1,M): start = M-i for j in xrange(start,M+1): #if i == j: continue s = i+j if gcd(i,j)!=1:continue #print "M:",M,i,j,s if s>=M: print "M:",M,i,j,s y = 1.0/(i*j) tot +=y return tot
def R(M): tot = 0.0 c = int(ceil(M / 2)) for i in xrange(1, M): start = M - i for j in xrange(start, M + 1): #if i == j: continue s = i + j if gcd(i, j) != 1: continue #print "M:",M,i,j,s if s >= M: print "M:", M, i, j, s y = 1.0 / (i * j) tot += y return tot
def R(M): tot = 0.0 ctr = 0 #c = int(ceil(M/2)) for i in xrange(1,M): #start = M-i for j in xrange(i+1,M+1): #if i == j: continue if gcd(i,j)!=1:continue s = i+j #print "M:",M,i,j,s if s>=M: print "M:",M,i,j,s y = 1.0/(i*j) tot +=y ctr +=1 return tot,ctr
def R(M): tot = 0.0 ctr = 0 #c = int(ceil(M/2)) for i in xrange(1, M): #start = M-i for j in xrange(i + 1, M + 1): #if i == j: continue if gcd(i, j) != 1: continue s = i + j #print "M:",M,i,j,s if s >= M: print "M:", M, i, j, s y = 1.0 / (i * j) tot += y ctr += 1 return tot, ctr
def p309(N = 10**6): lll = [[] for w in xrange(N)] for s in xrange(1, int(N**0.5) + 500): for t in xrange(1 + (s&1), s, 2): if s*s + t*t >= N: break if gcd(s, t) == 1: for l in xrange(1, N/(s*s + t*t) + 1 - ((N%(s*s + t*t)) == 0)): a = 2*l*s*t b = l*(s*s - t*t) lll[a].append(b) lll[b].append(a) count = 0 for ll in lll: for i, a in enumerate(ll): for b in ll[:i]: if (a*b) % (a+b) == 0: print a,b count += 1 return count
def isAchilles(l): s = set(l) prev = 0 psetlist=[] #hasOdds=False for v in s: z = l.count(v) if z < 2: return False #if z%2==1: hasOdds=True psetlist.append(z) if len(set(psetlist))==1: return False #if hasOdds==False: # return False 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
def p309(N=10**6): lll = [[] for w in xrange(N)] for s in xrange(1, int(N**0.5) + 500): for t in xrange(1 + (s & 1), s, 2): if s * s + t * t >= N: break if gcd(s, t) == 1: for l in xrange( 1, N / (s * s + t * t) + 1 - ((N % (s * s + t * t)) == 0)): a = 2 * l * s * t b = l * (s * s - t * t) lll[a].append(b) lll[b].append(a) count = 0 for ll in lll: for i, a in enumerate(ll): for b in ll[:i]: if (a * b) % (a + b) == 0: print a, b count += 1 return count
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
# # from time import time from math import floor from Functions import divisors,gcd st=time() N = 10**8 limit = int(N**.5) #ctr = 1 summ=0 summ2=0 for a in xrange(1,limit+1): for b in xrange(a,limit+1): if gcd(a,b)!=1:continue #a2,b2=pow(a,2),pow(b,2) a2,b2=a**2,b**2 div = a2 + b2 if div>N:break #ctr+=1 #No need for floor func. Runs slow, Int div in Python defaults to floor #Np=floor(N/div) if a==b: z=(a<<1) else: z=(a+b)<<1 # num0=div=(a^2+b^2) -> (num0, 2num0, 3num0....n*num0)/(div) == complex divisors # then take multiples of div, recalc floor and sum again for n in xrange(1, N/div +1):
# from Functions import gcd ''' A=[5248,1312,2624,5760,3936] B=[640,1888,3776,3776,5664] u0=1476;v0=1475 ''' LIMIT = 1000 i, j, k, t, t2, g, s, s2 = 0, 0, 0, 0, 0, 0, 0, 0 for i in xrange(1, LIMIT + 1): for j in xrange(1, LIMIT + 1): for k in xrange(1, LIMIT + 1): t = 6372 * (41 * i + 205 * j + 5 * k) t2 = 205 * (90 * i + 5310 * j + 59 * k) g = gcd(t, t2) t /= g t2 /= g s = int(t**.5) if s * s == t: s2 = int(t2**.5) if (s2 * s2 == t2 and s * 41 > 59 * s2 and s * i <= 37760 and s * j <= 11328 and s * k <= 339840): print i, j, k, s, s2
ctr=0 for d in range(5,D + 1): if d in primes: # IsPrime(d): strt = ceil(d*mn) endr = floor(d*mx) ctr += endr - strt + 1 else: #facts = RetFact(d) strt = ceil(d*mn) endr = floor(d*mx) #print "Comp D:",d,strt,endr+1 #g=RetFact(d) for i in xrange(int(strt),int(endr+1)): #a = RetFact(i) #if bool(set(a) & set(g))==False: #print i,":",a if gcd(i,d) ==1: ctr+=1 #print d,RetFact(i) #else: # print d,RetFact(i) print "Answer is",ctr print "Process time is", time()-st #short sol #len(set([float(a)/b for a in xrange(1,12001) for b in range(a+1,12001) if float(a)/b > 1.0/3 if float(a)/b < .5 ]))
D = 12000 f = RetFact(D) ctr = 0 for d in range(5, D + 1): if d in primes: # IsPrime(d): strt = ceil(d * mn) endr = floor(d * mx) ctr += endr - strt + 1 else: #facts = RetFact(d) strt = ceil(d * mn) endr = floor(d * mx) #print "Comp D:",d,strt,endr+1 #g=RetFact(d) for i in xrange(int(strt), int(endr + 1)): #a = RetFact(i) #if bool(set(a) & set(g))==False: #print i,":",a if gcd(i, d) == 1: ctr += 1 #print d,RetFact(i) #else: # print d,RetFact(i) print "Answer is", ctr print "Process time is", time() - st #short sol #len(set([float(a)/b for a in xrange(1,12001) for b in range(a+1,12001) if float(a)/b > 1.0/3 if float(a)/b < .5 ]))
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 print "Sum,ctr = ", summ, ctr print "time elapsed is", time() - st
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
# # from Functions import gcd Lim = 10000 #100000000 d = {} sum = 0 z = 0 for i in xrange(1, Lim + 1): sum += (Lim / i) * i for a in xrange(1, int(Lim**.5) + 1): for b in xrange(1, a + 1): # /*&& (a*a+b*b)<=Lim*/;b++) if (a, b) in d: z = d[(a, b)] else: d[(a, b)] = gcd(a, b) if (z == 1): i = (a * a + b * b) if (a == b): val = (a + b) else: val = 2 * (a + b) for j in xrange(1, int(Lim / i) + 1): sum += (j * val * (Lim / (i * j))) print sum
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) if v> 10**18: break A= isAchilles(n) if not A:continue phiA = Phi(v,n) fphi=RetFact(phiA) #F[phiA] #fphi=F[phiA] B = isAchilles(fphi) if not B:continue ctr+=1 if v>vv:ii,jj,vv=i,j,v if i>imax:imax=i if j>jmax:jmax=j print i,j,":" , v, fphi
# # # Euler Problem 75 # # from Functions import gcd from time import time from math import sqrt st = time() lim = 1500000 lim2 = int(sqrt(lim)) ar = [0] * (lim + 1) for m in xrange(1, lim2, 2): for n in xrange(2, lim2 - m, 2): if gcd(m, n) == 1: #print "m,n",m,n a = abs(m**2 - n**2) b = 2 * m * n c = m**2 + n**2 l = a + b + c #k=1 for s in range(l, lim, l): ar[s] += 1 ''' while l<=lim: #l *= k #if l>lim:break ar[l]+=1 l+=l
return s_local + s_smooth #------------------------------- # Complex divisors #------------------------------- def g_sum(n, p): '''Return the inner-most sum of complex Gaussian divisors with positive real part of all k, k=1..n. Using a summation formula. Runtime complexity: O(n^(1/2)).''' # Optimal split to balance local and smoth work q, s = int((n / p) ** 0.5), 0L # Local part for g in xrange(1L, n / (p * q) + 1): K = n / (p * g) s += (K * g + K * (K + 1) / 2) # Smooth part for k in xrange(1L, q): r1, r2 = n / (p * (k + 1)), n / (p * k) G = (r1 + r2 + 1) * (r2 - r1) / 2 s += (k * G + k * (k + 1) * (r2 - r1) / 2) return s '''Return the sum of complex Gaussian divisors with positive real part of all k, k=1..n. Using a summation formula. Runtime complexity: O(n).''' sum_divisors_complex = lambda n: \ sum(a * sum(g_sum(n, a * a + b * b) for b in xrange(1, int((n - a * a) ** 0.5) + 1) if gcd(a, b) == 1) for a in xrange(1, int((n - 1) ** 0.5) + 1)) sum_divisors_gaussian = lambda n: sum_divisors_rational(n) + sum_divisors_complex(n) if __name__ == "__main__": print sum_divisors_gaussian(10L ** 8)
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 print "Sum,ctr = ", summ, ctr print "time elapsed is", time() - st
# # # from Functions import gcd def A(n): curRepUnit = 1 k = 0 ctr = 1 while (curRepUnit % n != 0): curRepUnit = pow(10, ctr) / 9 k += 1 ctr += 1 return k ########################################## for n in xrange(1000020, 1000200): if gcd(n, 10) != 1: continue print n, A(n)
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
def totient(n): tot, pos = 0, n-1 while pos>0: if gcd(pos,n)==1: tot += 1 pos -= 1 return tot
from Functions import gcd from math import sqrt def primitive(a, b, c): if a%2 or b%2 or c%2: return (a, b, c) else: primitive(a, b, c) opt = [] count = 0 print 'Lijst met alle mogelijke rechthoekige 3hoeken genereren' for m in xrange(1, int(sqrt(0.5 * 15 * 10**5))): for n in xrange(1, m): if gcd(m, n) == 1 and (m - n) % 2: L = 2 * m * (m + n) for k in xrange(1, (15 * 10**5) // L + 1): L = k * 2 * m * (m + n) # a = m**2 - n**2 # b = 2*m*n # c = m**2 + n**2 # if a > b: # a, b = b, a # print a, b, c if L < 15 * 10**5: opt.append(L) # print L, m, n, k print 'Lijst gegenereerd ({0} items/{1} distinct), checken op multipliciteit'.format(len(opt), len(set(opt)))
# Euler Problem 75 # # from Functions import gcd from time import time from math import sqrt st = time() lim = 1500000 lim2 = int(sqrt(lim)) ar = [0] * (lim + 1) for m in xrange(1, lim2, 2): for n in xrange(2, lim2 - m, 2): if gcd(m, n) == 1: # print "m,n",m,n a = abs(m ** 2 - n ** 2) b = 2 * m * n c = m ** 2 + n ** 2 l = a + b + c # k=1 for s in range(l, lim, l): ar[s] += 1 """ while l<=lim: #l *= k #if l>lim:break ar[l]+=1
# # # # from Functions import gcd from time import time summ=0; Lim=100000 st=time() for i in xrange(1,Lim+1): # for(int i=1;i<=Lim;i++) summ+=(Lim/i)*i for a in xrange(1,int(Lim**.5)+1): #(int a=1;a*a<Lim;a++) for b in xrange(1,a+1): #(int b=1;b<=a /*&& (a*a+b*b)<=Lim*/;b++) if (gcd(a,b)==1): div=(a*a+b*b) if (a==b): val = (a+b) else: val=2*(a+b); for j in xrange(1, Lim/div +1): #(int j=1;i*j<=Lim;j++) : summ+=(j*val*(Lim/(div*j))) print summ print time()-st
# # # # from Functions import gcd from time import time summ = 0 Lim = 100000 st = time() for i in xrange(1, Lim + 1): # for(int i=1;i<=Lim;i++) summ += (Lim / i) * i for a in xrange(1, int(Lim**.5) + 1): #(int a=1;a*a<Lim;a++) for b in xrange(1, a + 1): #(int b=1;b<=a /*&& (a*a+b*b)<=Lim*/;b++) if (gcd(a, b) == 1): div = (a * a + b * b) if (a == b): val = (a + b) else: val = 2 * (a + b) for j in xrange(1, Lim / div + 1): #(int j=1;i*j<=Lim;j++) : summ += (j * val * (Lim / (div * j))) print summ print time() - st
def getDigitCount(n): # the first number to check is 1 current = 1 # 1 has a digit count of 1 digitCount = 1 # while n is not divisible by the current number while (current % n != 0): #print "current",current,current % n # we add one 1 to the end of current current = (current * 10 + 1) % n #and increment the digit count digitCount += 1 return digitCount ########################################## st = time() for i in xrange(1000020, 1000030): if gcd(i, 10) != 1: continue print i, getDigitCount(i) print "time elapsed is ", time() - st
# # from Functions import gcd Lim = 10000 #100000000 d={} sum=0 z=0 for i in xrange(1,Lim+1): sum+=(Lim/i)*i; for a in xrange(1,int(Lim**.5)+1): for b in xrange(1,a+1): # /*&& (a*a+b*b)<=Lim*/;b++) if (a,b) in d: z = d[(a,b)] else: d[(a,b)]=gcd(a,b) if(z==1): i=(a*a+b*b) if (a==b): val=(a+b) else: val=2*(a+b); for j in xrange(1,int(Lim/i)+1): sum+=(j*val*(Lim/(i*j))); print sum
def getDigitCount(n): # the first number to check is 1 current = 1; # 1 has a digit count of 1 digitCount = 1; # while n is not divisible by the current number while (current % n != 0): #print "current",current,current % n # we add one 1 to the end of current current = (current * 10 + 1)%n #and increment the digit count digitCount+=1 return digitCount ########################################## st = time() for i in xrange(1000020,1000030): if gcd(i,10)!=1:continue print i, getDigitCount(i) print "time elapsed is ", time()-st
# from Functions import gcd ''' A=[5248,1312,2624,5760,3936] B=[640,1888,3776,3776,5664] u0=1476;v0=1475 ''' LIMIT = 1000 i,j,k,t,t2,g,s,s2=0,0,0,0,0,0,0,0 for i in xrange(1,LIMIT+1): for j in xrange(1,LIMIT+1): for k in xrange(1,LIMIT+1): t = 6372*(41*i+205*j+5*k) t2 = 205*(90*i+5310*j+59*k) g = gcd(t,t2) t /= g t2 /= g s = int(t**.5) if s*s == t: s2 = int(t2**.5) if (s2*s2 == t2 and s*41 > 59*s2 and s*i <= 37760 and s*j <= 11328 and s*k <= 339840): print i,j,k,s,s2
# # # from Functions import gcd def A(n): curRepUnit = 1; k = 0; ctr=1 while (curRepUnit%n != 0): curRepUnit = pow(10,ctr)/9 k+=1 ctr+=1 return k ########################################## for n in xrange(1000020,1000200): if gcd(n,10)!=1:continue print n, A(n)
# # Euler 73 # from Functions import gcd from math import ceil from time import time st=time() ctr = -2 d={} # mini = 1./3. # maxi = .5 for i in xrange(1,12001): x=int(ceil(i/3.)) #print x ,i for j in xrange(x,int(i/2)+1 ): #val = (1.0*j)/i #print x if gcd(i,j) == 1: # if not( val>mini and val < maxi): # print i,j # continue if i*12000+j not in d: ctr +=1 d[i*12000+j]=1 print "total is ", ctr print "time elapsed is ", time()-st
if gcd(i, j) != 1: continue s = i + j #print "M:",M,i,j,s if s >= M: print "M:", M, i, j, s y = 1.0 / (i * j) tot += y ctr += 1 return tot, ctr ctr = 0 d = {} summ = 0 for i in xrange(2, 10001): for j in xrange(i + 1, 10001): if gcd(i, j) == 1: ctr += 1 #print i,j # d[(i,j)]=(1.0/(i*j)) print #print d print "Grand Total is ", summ print "time elapsed is ", time() - st # print # print R(1000)