pyzuki

pickle_attempt_for_web.py

Aug 11th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.27 KB | None | 0 0
  1. # pickle_attempt_for_web.py
  2.  
  3. import pickle
  4.  
  5. def findFactor(n, startingAt):
  6.     """
  7.    Written by Kent Johnson
  8.    """
  9.     limit = int(n**.5) + 1
  10.     x = startingAt
  11.     while x < limit:
  12.         if not n % x:
  13.             return x
  14.         x += 2
  15.     return False
  16.  
  17. def factorsOfInteger(n):
  18.     """
  19.    Return list of prime factors of n.
  20.    Returns [n] if n is prime.
  21.    Re-written largely by Kent Johnson
  22.    """
  23.     factors = []
  24.     if n == 1:
  25.         return [1]
  26.     # Get the factors of two out of the way to simplify findFactor
  27.     while n % 2 == 0:
  28.         factors.append(2)
  29.         n = n // 2
  30.     lastFactor = 3
  31.     r = n
  32.     while True:
  33.         factor = findFactor(r, lastFactor)
  34.         if not factor:
  35.             # r is prime
  36.             if r == 1: # This is needed, at least for powers of 2
  37.                 break
  38.             factors.append(r)
  39.             break
  40.         factors.append(factor)
  41.         lastFactor = factor
  42.         r = r / factor
  43.     if len(factors) == 1:
  44.         return factors
  45.     factors[-1] = int(factors[-1]) # without this some last factors are floats, ending in .0
  46.     return factors
  47.  
  48. D = {}
  49. f = open("factors.txt", 'rb')
  50. data = pickle.load(f)
  51. f.close
  52. D = data
  53.        
  54. begin = 4000000
  55. w = 10
  56. end = begin + w
  57. for n in range(begin, end):
  58.     factors_list = factorsOfInteger(n)
  59.     D[n] = factors_list
  60.  
  61. print()
  62.  
  63. while True:
  64.     n = int(input("integer: "))
  65.            
  66.     if n == 0:
  67.         print("D has", len(D), "entries")
  68.         f = open("factors.txt", 'ab')
  69.         pickle.dump(D, f)
  70.         f.close
  71.         break
  72.    
  73.     try:
  74.         factors = D[n]
  75.         print("the factors of", n, "are", factors)
  76.        
  77.     except KeyError:
  78.         factors = factorsOfInteger(n)
  79.         print("the factors of", n, "are", factors)
  80.         D[n] = factors
  81.        
  82.     print(D.items())
  83.     print()
  84.     print("D has", len(D), "entries")
  85.    
  86. #===================OUTPUTS using lines 54 through 59=======================
  87.  
  88. #integer: 4
  89. #the factors of 4 are [2, 2]
  90. #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  91.             #(1000001, [101, 9901]),
  92.             #(1000002, [2, 3, 166667]),
  93.             #(1000003, [1000003]),
  94.             #(1000004, [2, 2, 53, 53, 89]),
  95.             #(1000005, [3, 5, 163, 409]),
  96.             #(1000006, [2, 7, 71429]),
  97.             #(1000007, [29, 34483]),
  98.             #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
  99.             #(1000009, [293, 3413]),
  100.            
  101.             #(2000000, [2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  102.             #(2000007, [3, 3, 61, 3643]),
  103.             #(2000002, [2, 101, 9901]),
  104.             #(2000001, [3, 666667]),
  105.             #(2000008, [2, 2, 2, 53, 53, 89]),
  106.             #(2000003, [2000003]),
  107.             #(4, [2, 2]),
  108.             #(2000009, [11, 11, 16529]),
  109.             #(2000004, [2, 2, 3, 166667]),
  110.             #(2000006, [2, 1000003]),
  111.             #(2000005, [5, 7, 57143])])
  112.  
  113. #D has 21 entries
  114. #integer: 0
  115. #D has 21 entries
  116. #===================OUTPUTS using lines 54 through 59=======================
  117.  
  118. #integer: 4
  119. #the factors of 4 are [2, 2]
  120. #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  121.             #(1000001, [101, 9901]),
  122.             #(1000002, [2, 3, 166667]),
  123.             #(1000003, [1000003]),
  124.             #(1000004, [2, 2, 53, 53, 89]),
  125.             #(1000005, [3, 5, 163, 409]),
  126.             #(1000006, [2, 7, 71429]),
  127.             #(1000007, [29, 34483]),
  128.             #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
  129.             #(1000009, [293, 3413]),
  130.            
  131.             #(3000007, [17, 109, 1619]),
  132.             #(3000002, [2, 557, 2693]),
  133.             #(3000006, [2, 3, 3, 166667]),
  134.             #(3000008, [2, 2, 2, 11, 73, 467]),
  135.             #(3000003, [3, 101, 9901]),
  136.             #(3000009, [3, 1000003]),
  137.             #(3000004, [2, 2, 7, 307, 349]),
  138.             #(3000001, [853, 3517]),
  139.             #(3000000, [2, 2, 2, 2, 2, 2, 3, 5, 5, 5, 5, 5, 5]),
  140.             #(4, [2, 2]), (3000005, [5, 19, 23, 1373])])
  141.  
  142. #D has 21 entries
  143. #integer: 0
  144. #D has 21 entries
  145. #===================OUTPUTS using lines 54 through 59=======================
  146.  
  147. #integer: 4
  148. #the factors of 4 are [2, 2]
  149. #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  150.             #(1000001, [101, 9901]),
  151.             #(1000002, [2, 3, 166667]),
  152.             #(1000003, [1000003]),
  153.             #(1000004, [2, 2, 53, 53, 89]),
  154.             #(1000005, [3, 5, 163, 409]),
  155.             #(1000006, [2, 7, 71429]),
  156.             #(1000007, [29, 34483]),
  157.             #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
  158.             #(1000009, [293, 3413]),
  159.            
  160.             #(4000007, [11, 79, 4603]),
  161.             #(4000001, [41, 97561]),
  162.             #(4000002, [2, 3, 666667]),
  163.             #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  164.             #(4000008, [2, 2, 2, 3, 166667]),
  165.             #(4000003, [7, 139, 4111]), (4000009, [13, 307693]),
  166.             #(4000004, [2, 2, 101, 9901]),
  167.             #(4000006, [2, 2000003]), (4, [2, 2]),
  168.             #(4000005, [3, 3, 5, 103, 863])])
  169.  
  170. #D has 21 entries
  171. #integer: 0
  172. #D has 21 entries
  173.  
  174. #==========OUTPUTS using only lines 78 and 80 to add items=========
  175.  
  176. #integer: 100
  177. #the factors of 100 are [2, 2, 5, 5]
  178. #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  179.             #(1000001, [101, 9901]),
  180.             #(1000002, [2, 3, 166667]),
  181.             #(1000003, [1000003]),
  182.             #(1000004, [2, 2, 53, 53, 89]),
  183.             #(1000005, [3, 5, 163, 409]),
  184.             #(1000006, [2, 7, 71429]),
  185.             #(1000007, [29, 34483]),
  186.             #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
  187.             #(1000009, [293, 3413]),
  188.             #(4000007, [11, 79, 4603]),
  189.             #(4000001, [41, 97561]),
  190.             #(4000002, [2, 3, 666667]),
  191.             #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  192.             #(4000008, [2, 2, 2, 3, 166667]),
  193.             #(4000003, [7, 139, 4111]),
  194.             #(4000009, [13, 307693]),
  195.             #(100, [2, 2, 5, 5]),
  196.             #(4000004, [2, 2, 101, 9901]),
  197.             #(4000006, [2, 2000003]),
  198.             #(4000005, [3, 3, 5, 103, 863])])
  199.  
  200. #D has 21 entries
  201.  
  202. #integer: 101
  203. #the factors of 101 are [101]
  204. #dict_items([(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  205.             #(4000001, [41, 97561]),
  206.             #(4000002, [2, 3, 666667]),
  207.             #(4000003, [7, 139, 4111]),
  208.             #(4000004, [2, 2, 101, 9901]),
  209.             #(4000005, [3, 3, 5, 103, 863]),
  210.             #(4000006, [2, 2000003]),
  211.             #(4000007, [11, 79, 4603]),
  212.             #(4000008, [2, 2, 2, 3, 166667]),
  213.             #(4000009, [13, 307693]),
  214.             #(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  215.             #(1000001, [101, 9901]),
  216.             #(1000002, [2, 3, 166667]),
  217.             #(1000003, [1000003]),
  218.             #(1000004, [2, 2, 53, 53, 89]),
  219.             #(1000005, [3, 5, 163, 409]),
  220.             #(1000006, [2, 7, 71429]),
  221.             #(1000007, [29, 34483]),
  222.             #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
  223.             #(1000009, [293, 3413]),
  224.             #(100, [2, 2, 5, 5]),
  225.             #(101, [101])])
  226.  
  227. #D has 22 entries
  228.  
  229. #integer: 103
  230. #the factors of 103 are [103]
  231. #dict_items([(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  232.             #(4000001, [41, 97561]),
  233.             #(4000002, [2, 3, 666667]),
  234.             #(4000003, [7, 139, 4111]),
  235.             #(4000004, [2, 2, 101, 9901]),
  236.             #(4000005, [3, 3, 5, 103, 863]),
  237.             #(4000006, [2, 2000003]),
  238.             #(4000007, [11, 79, 4603]),
  239.             #(4000008, [2, 2, 2, 3, 166667]),
  240.             #(4000009, [13, 307693]),
  241.             #(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
  242.             #(1000001, [101, 9901]), (1000002, [2, 3, 166667]),
  243.             #(1000003, [1000003]),
  244.             #(1000004, [2, 2, 53, 53, 89]),
  245.             #(1000005, [3, 5, 163, 409]),
  246.             #(1000006, [2, 7, 71429]),
  247.             #(1000007, [29, 34483]),
  248.             #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
  249.             #(1000009, [293, 3413]),
  250.             #(100, [2, 2, 5, 5]),
  251.             #(101, [101]),
  252.             #(103, [103])])
  253.  
  254. #D has 23 entries
  255. #integer: 0
  256. #D has 23 entries
Advertisement
Add Comment
Please, Sign In to add comment