Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import math
  2. import sympy.ntheory as spn
  3.  
  4. powers = {}
  5. powers[2] = 39
  6. powers[3] = 19
  7. powers[5] = 9
  8. powers[7] = 6
  9. powers[11] = 3
  10. powers[13] = 3
  11. powers[17] = 2
  12. powers[19] = 2
  13. powers[23] = 1
  14. powers[29] = 1
  15. powers[31] = 1
  16. powers[37] = 1
  17. powers[41] = 1
  18. powers[43] = 1
  19.  
  20. limit = math.factorial(43)
  21. primes = sorted(list(powers.keys()))
  22.  
  23. allProds = set()
  24. memo = {}
  25. print(limit)
  26.  
  27. def findC(ab):
  28.     tp = tuple()
  29.     res = 1
  30.     for p in primes:
  31.         i = 1
  32.         while ab % pow(p,i) == 0:
  33.             i += 1
  34.         i -= 1
  35.         if i > powers[p]: return 0
  36.         tp = tp + (powers[p]-i,)
  37.     for i in range(len(primes)):
  38.         res *= pow(primes[i], tp[i])
  39.     return res
  40.  
  41. def bfs(currTpls, ind, lastProd):
  42.     if ind == len(primes): return
  43.     print(ind, len(allProds), len(currTpls))
  44.     newTpls = []
  45.     p = primes[ind]
  46.     for tp in currTpls:
  47.         if pow(tp*lastProd,3) >= limit - int(pow(10,15)):
  48.             for i in range(powers[p]+1):
  49.                 if pow(tp * pow(p,i)-pow(10,13),3) <= limit:
  50.                     newTpls.append(tp*pow(p, i))
  51.                     if pow(tp*pow(p,i)+pow(10,13),3) >= limit:
  52.                         allProds.add(tp*pow(p,i))
  53.                 else:
  54.                     break
  55.         else:
  56.             break
  57.     newTpls = sorted(newTpls, reverse=True)
  58.     return bfs(newTpls, ind+1, lastProd//pow(p,powers[p]))
  59.  
  60. currTpls = []
  61. for i in range(powers[2]+1):
  62.     memo[pow(2,i)] = (i)
  63.     currTpls.append(pow(2,i))
  64.  
  65. bfs(currTpls, 1, limit//pow(2,powers[2]))
  66. print(len(allProds))
  67.  
  68. allProdsList = list(allProds)
  69. allProdsList.sort()
  70.  
  71. minRatio = 10000
  72. minTp = []
  73. for i in range(len(allProdsList)):
  74.     a = allProdsList[i]
  75.     for j in range(i, len(allProdsList)):
  76.         b = allProdsList[j]
  77.         c = findC(a*b)
  78.         if b <= c:
  79.             if c/a < minRatio:
  80.                 minTp = [a,b,c]
  81.                 minRatio = c/a
  82.     print(i, minRatio, minTp)
  83.  
  84. print(minRatio)
  85. print(minTp)
  86. print(sum(minTp))
  87. print(limit, a*b*c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement