Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def A(lastn,mode):
- ## mode 0: print all factorizations for a(n). There might be other combinations possible, but the programm should always find the maximum number of them
- ## mode 1: mode 0 + print leftover factors which could not be included in a factorization triplet, quadruplet... etc. (factors building a factorization pair with a prime already sorted out)
- ## mode 2: print only n,a(n)
- n,a=0,[1]
- while n<lastn:
- i,s,v,factors=2,str(a.count(a[n]))+"x"+str(a[n]),a.count(a[n]),[]
- while i<a[n]**(0.5):
- j=a[n]//i
- if j==a[n]/i:
- for x in (x for x in a if (x==i and a.count(i)>a.count(j)+factors.count(i)) or (x==j and a.count(j)>a.count(i)+factors.count(j))):
- ii=2
- while ii<=(a[n]/x)**(0.5)+0.1:
- if (a[n]/x)//ii==(a[n]/x)/ii:
- factors.append(x)
- break
- ii+=1
- v,s=v+min(a.count(i),a.count(j)),s+", "+str(min(a.count(i),a.count(j)))+"x"+str(i)+"*"+str(j)
- i+=1
- if a[n]/i==i:
- sq=a.count(i)//2
- if sq>0: v,s=v+sq,s+", "+str(sq)+"x"+str(i)+"*"+str(i)
- if a.count(i)/2>sq and len(factors)>0: factors.append(i)
- st,sp,factoriz,nu,x="",[],[],[a[n]],[]
- if a[n]>7 and len(factors)>2:
- flist,r,q=list(set(factors[:])),0,[]
- x.append(len(flist)-1)
- while r>=0:
- if nu[r]/flist[x[r]]==int(nu[r]/flist[x[r]]) and q.count(flist[x[r]])<factors.count(flist[x[r]]):
- q.append(flist[x[r]])
- if nu[r]/flist[x[r]]==1:
- factoriz.append(q[:])
- q.pop(r)
- x[r]-=1
- else:
- r=r+1
- nu.append(nu[r-1]/flist[x[r-1]])
- x.append(x[r-1])
- else:
- x[r]-=1
- while x[r]<0:
- r-=1
- if r<0: break
- q.pop(r)
- nu.pop(r+1)
- x.pop(r+1)
- x[r]-=1
- if len(factoriz)>0:
- ##print(factoriz)
- fz,f,q,r,x=[factoriz[:]],[factors[:]],[[]],0,[len(factoriz)-1],
- while r>=0:
- j,c=len(fz[r][x[r]])-1,int(f[r].count(fz[r][x[r]][0])/fz[r][x[r]].count(fz[r][x[r]][0]))
- while j>=1:
- c=min(c,int(f[r].count(fz[r][x[r]][j])/fz[r][x[r]].count(fz[r][x[r]][j])))
- j-=1
- if c>0:
- q.append(q[r][:])
- f.append(f[r][:])
- fz.append(fz[r][:])
- r+=1
- while c>0:
- q[r].append(fz[r-1][x[r-1]])
- k=len(fz[r-1][x[r-1]])-1
- while k>=0:
- f[r].remove(fz[r-1][x[r-1]][k])
- k-=1
- c-=1
- fz[r].remove(fz[r-1][x[r-1]])
- x.append(len(fz[r])-1)
- else:
- x[r]-=1
- while x[r]<0:
- r-=1
- if r<0: break
- fz.pop(r+1)
- f.pop(r+1)
- if len(q[r+1])>len(sp): sp=q[r+1][:]
- q.pop(r+1)
- x.pop(r+1)
- x[r]-=1
- a.append(v+len(sp))
- if a[n]<1000:
- s=" "+s
- if a[n]<100:
- s=" "+s
- if a[n]<10:
- s=" "+s
- if mode!=2:
- if len(sp)>0:
- si=len(sp)-1
- while si>-1:
- ssi=len(sp[si])-1
- sv=sp.count(sp[si])
- s+=", "+str(sv)+"x"
- while ssi>-1:
- if ssi>0: s+=str(sp[si][ssi])+"*"
- else: s+=str(sp[si][ssi])
- sf=0
- while sf<sv:
- factors.remove(sp[si][ssi])
- sf+=1
- ssi-=1
- si-=sv
- if mode==1 and len(factors)>0:
- si=0
- s+=" "+"("
- while si<len(factors):
- sv=factors.count(factors[si])
- if si==0: s+=str(sv)+"x"+str(factors[si])
- else: s+=", "+str(sv)+"x"+str(factors[si])
- si+=sv
- s+=")"
- print(str(n+1)+" "+str(a[n])+" "+s)
- else: print(str(n+1)+" "+str(a[n]))
- n+=1
- return a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement