Advertisement
pyzuki

pickle_attempt_for_web2.py

Aug 12th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.17 KB | None | 0 0
  1. # pickle_attempt_for_web2.py
  2.  
  3. from time import time as t
  4. import pickle
  5. # Should gmpy2 be unavailable, just remark out the highlighted lines.
  6. import gmpy2
  7. import random
  8.  
  9. def int_commas(n):
  10.     """
  11.    inserts commas into integers and returns the string
  12.  
  13.    E.g. -12345678 -> -12,345,789
  14.    """
  15.     return format(n, ',d')
  16.  
  17. def printTime(timeStart, timeEnd):
  18.     from mycalc import hmsToText
  19.     timeElapsed = timeEnd - timeStart
  20.     if timeElapsed > 60:
  21.         print("Time was", hmsToText(timeElapsed))
  22.     else:
  23.         print("Time was %.4g seconds" % timeElapsed)
  24.  
  25. def randIntOfGivenLength(length):
  26.     """
  27.    Return a random int of a given number of digits
  28.    """
  29.     return random.randint(10**(length-1), 10**length - 1)
  30.  
  31. def findFactor(n, startingAt):
  32.     """
  33.    Written by Kent Johnson
  34.    """
  35.     limit = int(n**.5) + 1
  36.     x = startingAt
  37.     while x < limit:
  38.         if not n % x:
  39.             return x
  40.         x += 2
  41.     return False
  42.  
  43. def factorsOfInteger(n):
  44.     """
  45.    Return list of prime factors of n.
  46.    Returns [n] if n is prime.
  47.    Re-written largely by Kent Johnson
  48.    """
  49.     factors = []
  50.     if n == 1:
  51.         return [1]
  52.     # Get the factors of two out of the way to simplify findFactor
  53.     while n % 2 == 0:
  54.         factors.append(2)
  55.         n = n // 2
  56.     lastFactor = 3
  57.     r = n
  58.     while True:
  59.         factor = findFactor(r, lastFactor)
  60.         if not factor:
  61.             # r is prime
  62.             if r == 1: # This is needed, at least for powers of 2
  63.                 break
  64.             factors.append(r)
  65.             break
  66.         factors.append(factor)
  67.         lastFactor = factor
  68.         r = r / factor
  69.     if len(factors) == 1: # changed this from  if n in factors
  70.         return factors
  71.     factors[-1] = int(factors[-1]) # without this some last factors are floats, ending in .0
  72.     return factors
  73.  
  74. D = {}
  75. f = open("factors.dat", 'rb')
  76. data = pickle.load(f)
  77. f.close
  78. D = data
  79.        
  80. begin = 2000000000
  81. w = 1000
  82. end = begin + w
  83.  
  84. add_flag = 0
  85. if add_flag == 1:
  86.     t0 = t()
  87.     for n in range(begin, end):
  88.         factors_list = factorsOfInteger(n)
  89.         D[n] = factors_list
  90.     t1 = t()
  91.     printTime(t0,t1)
  92.  
  93. print()
  94.  
  95. while True:
  96.     oldnew_flag = 0
  97.     print("Enter a positive integer OR")
  98.     print("Enter nothing to generate an 18-digit integer OR")
  99.     print("Enter a zero to close the program.")
  100.     print()
  101.     s = input("> ")
  102.    
  103.     if "10**" in s:
  104.         n = 10**(int(s[4:]))
  105.         plus = input("plus: ")
  106.         if plus == '':
  107.             plus = 0
  108.         else:
  109.             plus = int(plus)
  110.         n += plus
  111.        
  112.        
  113.     elif s != '':
  114.         n = int(s)
  115.        
  116.         if n >= 10**19:
  117.             print(n, "is too big: calculation might take too long.")
  118.             print("Choose another n")
  119.             print()
  120.             continue
  121.        
  122.         elif gmpy2.is_prime(n) and len(str(n)) >= 18:
  123.             print(n, "is prime -- will take too long.")
  124.             print("Choose another n")
  125.             print()
  126.             continue  
  127.        
  128.     elif s == '':
  129.         n = randIntOfGivenLength(18)
  130.         print("n =", n, "a random 18-digit integer")
  131.         if gmpy2.is_prime(n):
  132.             print(n, "is prime -- will take too long.")
  133.             print("Choose another n")
  134.             print()
  135.             continue
  136.        
  137.     if n == 0:
  138.         print("D has", int_commas(len(D)), "entries")
  139.         f = open("factors.dat", 'wb')
  140.         pickle.dump(D, f)
  141.         f.close
  142.         break
  143.    
  144.     t0 = t()
  145.    
  146.     try:
  147.         factors = D[n]
  148.         print("the factors of", int_commas(n), "are", factors)
  149.         oldnew_flag = 1
  150.         print("OLD")
  151.        
  152.     except KeyError:
  153.         factors = factorsOfInteger(n)
  154.         print("the factors of", int_commas(n), "are", factors)
  155.         D[n] = factors
  156.         oldnew_flag = 2
  157.         print("NEW")
  158.        
  159.     t1 = t()
  160.    
  161.     print()
  162.    
  163.     #print(D.items())
  164.     #print(D.keys())
  165.     printTime(t0,t1)
  166.     if oldnew_flag == 1:
  167.         print("OLD ENTRY")
  168.     elif oldnew_flag == 2:
  169.         print("NEW ENTRY")
  170.     print("D has", int_commas(len(D)), "entries")
  171.     print()
  172.    
  173. #=========Output of one session==============
  174. #Enter a positive integer OR
  175. #Enter nothing to generate an 18-digit integer OR
  176. #Enter a zero to close the program.
  177.  
  178. #>
  179. #n = 279918016070854658 a random 18-digit integer
  180. #the factors of 279,918,016,070,854,658 are [2, 7, 41, 487662048903928]
  181. #NEW
  182.  
  183. #Time was 4.13 seconds
  184. #NEW ENTRY
  185. #D has 2 entries
  186.  
  187. #Enter a positive integer OR
  188. #Enter nothing to generate an 18-digit integer OR
  189. #Enter a zero to close the program.
  190.  
  191. #> 1234
  192. #the factors of 1,234 are [2, 617]
  193. #NEW
  194.  
  195. #Time was 0.0009999 seconds
  196. #NEW ENTRY
  197. #D has 3 entries
  198.  
  199. #Enter a positive integer OR
  200. #Enter nothing to generate an 18-digit integer OR
  201. #Enter a zero to close the program.
  202.  
  203. #> 3456
  204. #the factors of 3,456 are [2, 2, 2, 2, 2, 2, 2, 3, 3, 3]
  205. #NEW
  206.  
  207. #Time was 0 seconds
  208. #NEW ENTRY
  209. #D has 4 entries
  210.  
  211. #Enter a positive integer OR
  212. #Enter nothing to generate an 18-digit integer OR
  213. #Enter a zero to close the program.
  214.  
  215. #> 3456
  216. #the factors of 3,456 are [2, 2, 2, 2, 2, 2, 2, 3, 3, 3]
  217. #OLD
  218.  
  219. #Time was 0.001 seconds
  220. #OLD ENTRY
  221. #D has 4 entries
  222.  
  223. #Enter a positive integer OR
  224. #Enter nothing to generate an 18-digit integer OR
  225. #Enter a zero to close the program.
  226.  
  227. #>
  228. #n = 668979809228717177 a random 18-digit integer
  229. #the factors of 668,979,809,228,717,177 are [2767, 111031, 2177507201]
  230. #NEW
  231.  
  232. #Time was 0.027 seconds
  233. #NEW ENTRY
  234. #D has 5 entries
  235.  
  236. #Enter a positive integer OR
  237. #Enter nothing to generate an 18-digit integer OR
  238. #Enter a zero to close the program.
  239.  
  240. #> 0
  241. #D has 5 entries
  242.  
  243. #===============Output of a new session================
  244. # added 1000 new entries from 2000000000 to 2000001000 using lines 83-90
  245.  
  246. #Time was 1.277 seconds
  247.  
  248. #Enter a positive integer OR
  249. #Enter nothing to generate an 18-digit integer OR
  250. #Enter a zero to close the program.
  251.  
  252. #> 0
  253. #D has 1,005 entries
  254.  
  255. #===============Output of a new session================
  256. # I set the add_flag to 0 (line 83) so lines 83-90 will be ignored
  257.  
  258. #Enter a positive integer OR
  259. #Enter nothing to generate an 18-digit integer OR
  260. #Enter a zero to close the program.
  261.  
  262. #> 8756765
  263. #the factors of 8,756,765 are [5, 1751353]
  264. #NEW
  265.  
  266. #Time was 0.0009999 seconds
  267. #NEW ENTRY
  268. #D has 1,006 entries
  269.  
  270. #Enter a positive integer OR
  271. #Enter nothing to generate an 18-digit integer OR
  272. #Enter a zero to close the program.
  273.  
  274. #>
  275. #n = 158970453260687288 a random 18-digit integer
  276. #the factors of 158,970,453,260,687,288 are [2, 2, 2, 11, 67, 157, 251, 6397, 106957]
  277. #NEW
  278.  
  279. #Time was 0.002 seconds
  280. #NEW ENTRY
  281. #D has 1,007 entries
  282.  
  283. #Enter a positive integer OR
  284. #Enter nothing to generate an 18-digit integer OR
  285. #Enter a zero to close the program.
  286.  
  287. #> 1111111111111
  288. #the factors of 1,111,111,111,111 are [53, 79, 265371653]
  289. #NEW
  290.  
  291. #Time was 0.01 seconds
  292. #NEW ENTRY
  293. #D has 1,008 entries
  294.  
  295. #Enter a positive integer OR
  296. #Enter nothing to generate an 18-digit integer OR
  297. #Enter a zero to close the program.
  298.  
  299. #> 0
  300. #D has 1,008 entries
  301.  
  302. #==================END=========================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement