Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. def A(lastn,mode):
  2. ## 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
  3. ## 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)
  4. ## mode 2: print only n,a(n)
  5. n,a=0,[1]
  6. while n<lastn:
  7. i,s,v,factors=2,str(a.count(a[n]))+"x"+str(a[n]),a.count(a[n]),[]
  8. while i<a[n]**(0.5):
  9. j=a[n]//i
  10. if j==a[n]/i:
  11. 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))):
  12. ii=2
  13. while ii<=(a[n]/x)**(0.5)+0.1:
  14. if (a[n]/x)//ii==(a[n]/x)/ii:
  15. factors.append(x)
  16. break
  17. ii+=1
  18. v,s=v+min(a.count(i),a.count(j)),s+", "+str(min(a.count(i),a.count(j)))+"x"+str(i)+"*"+str(j)
  19. i+=1
  20. if a[n]/i==i:
  21. sq=a.count(i)//2
  22. if sq>0: v,s=v+sq,s+", "+str(sq)+"x"+str(i)+"*"+str(i)
  23. if a.count(i)/2>sq and len(factors)>0: factors.append(i)
  24. st,sp,factoriz,nu,x="",[],[],[a[n]],[]
  25. if a[n]>7 and len(factors)>2:
  26. flist,r,q=list(set(factors[:])),0,[]
  27. x.append(len(flist)-1)
  28. while r>=0:
  29. if nu[r]/flist[x[r]]==int(nu[r]/flist[x[r]]) and q.count(flist[x[r]])<factors.count(flist[x[r]]):
  30. q.append(flist[x[r]])
  31. if nu[r]/flist[x[r]]==1:
  32. factoriz.append(q[:])
  33. q.pop(r)
  34. x[r]-=1
  35. else:
  36. r=r+1
  37. nu.append(nu[r-1]/flist[x[r-1]])
  38. x.append(x[r-1])
  39. else:
  40. x[r]-=1
  41. while x[r]<0:
  42. r-=1
  43. if r<0: break
  44. q.pop(r)
  45. nu.pop(r+1)
  46. x.pop(r+1)
  47. x[r]-=1
  48. if len(factoriz)>0:
  49. ##print(factoriz)
  50. fz,f,q,r,x=[factoriz[:]],[factors[:]],[[]],0,[len(factoriz)-1],
  51. while r>=0:
  52. 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]))
  53. while j>=1:
  54. c=min(c,int(f[r].count(fz[r][x[r]][j])/fz[r][x[r]].count(fz[r][x[r]][j])))
  55. j-=1
  56. if c>0:
  57. q.append(q[r][:])
  58. f.append(f[r][:])
  59. fz.append(fz[r][:])
  60. r+=1
  61. while c>0:
  62. q[r].append(fz[r-1][x[r-1]])
  63. k=len(fz[r-1][x[r-1]])-1
  64. while k>=0:
  65. f[r].remove(fz[r-1][x[r-1]][k])
  66. k-=1
  67. c-=1
  68. fz[r].remove(fz[r-1][x[r-1]])
  69. x.append(len(fz[r])-1)
  70. else:
  71. x[r]-=1
  72. while x[r]<0:
  73. r-=1
  74. if r<0: break
  75. fz.pop(r+1)
  76. f.pop(r+1)
  77. if len(q[r+1])>len(sp): sp=q[r+1][:]
  78. q.pop(r+1)
  79. x.pop(r+1)
  80. x[r]-=1
  81. a.append(v+len(sp))
  82. if a[n]<1000:
  83. s=" "+s
  84. if a[n]<100:
  85. s=" "+s
  86. if a[n]<10:
  87. s=" "+s
  88. if mode!=2:
  89. if len(sp)>0:
  90. si=len(sp)-1
  91. while si>-1:
  92. ssi=len(sp[si])-1
  93. sv=sp.count(sp[si])
  94. s+=", "+str(sv)+"x"
  95. while ssi>-1:
  96. if ssi>0: s+=str(sp[si][ssi])+"*"
  97. else: s+=str(sp[si][ssi])
  98. sf=0
  99. while sf<sv:
  100. factors.remove(sp[si][ssi])
  101. sf+=1
  102. ssi-=1
  103. si-=sv
  104. if mode==1 and len(factors)>0:
  105. si=0
  106. s+=" "+"("
  107. while si<len(factors):
  108. sv=factors.count(factors[si])
  109. if si==0: s+=str(sv)+"x"+str(factors[si])
  110. else: s+=", "+str(sv)+"x"+str(factors[si])
  111. si+=sv
  112. s+=")"
  113. print(str(n+1)+" "+str(a[n])+" "+s)
  114. else: print(str(n+1)+" "+str(a[n]))
  115. n+=1
  116. return a
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement