pyzuki

factor_random_integers.py

Aug 22nd, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.07 KB | None | 0 0
  1. # factor_random_integers.py
  2.  
  3. def randIntOfRandLength(minLength, maxLength):
  4.     """
  5.    First, the function generates a random length in closed interval
  6.    [minLength, maxLength]. Then it generates a random integer n of that length,
  7.    i.e., that many digits. The important feature of this function is that
  8.    lengths within the max and min are all equally likely, as opposed to
  9.    functions such as random.randint(). For example, for
  10.    random.randint(10, 999), integers of 3-digit integers are 10 times more
  11.    likely to be generated than 2-digit integers.
  12.    """
  13.     import random
  14.     x = None
  15.     s = ""
  16.     n = None
  17.     length = random.randint(minLength, maxLength)
  18.     while True:
  19.         x = random.randint(0, 9)
  20.         s = s + str(x)
  21.         if s[0] == '0':
  22.             s.lstrip('0')
  23.         n = int(s)
  24.         if n >= 10**(length - 1):
  25.             return n
  26.  
  27. def load_pickled_data(path='C:\P32Working\Data\primes_to_100_million.dat'):
  28.     import pickle
  29.     import os.path
  30.     import io        
  31.        
  32.     if os.path.getsize(path) == 0:
  33.         #The file exists, but is empty: leave it alone
  34.         pass
  35.    
  36.     elif os.path.exists(path):
  37.         #The file exists and is not empty: use it
  38.         with open(path, "rb") as f:
  39.             data = pickle.load(f)
  40.     print("That f is closed now is", f.closed)      
  41.    
  42.     D = data
  43.     primes = D
  44.     return primes    
  45.  
  46. def int_factors(n, primes):
  47.     import gmpy2
  48.     from gmpy2 import is_prime  
  49.    
  50.     factors = []
  51.     limit = int(n**.5) + 1
  52.     if is_prime(n):
  53.         return [int(n)]
  54.  
  55.     if n == 1:
  56.         return []
  57.    
  58.     for x in primes:
  59.         while n % x == 0:
  60.             factors.append(int(x))
  61.             n = n // x
  62.             if is_prime(n):
  63.                 factors.append(int(n))
  64.                 return factors
  65.            
  66.     while True:
  67.        
  68.         x += 2
  69.        
  70.         while n % x == 0:
  71.             factors.append(int(x))
  72.             n = n // x
  73.             if is_prime(n):
  74.                 factors.append(int(n))
  75.                 return factors
  76.            
  77. def test_of_list_of_prime_factors_of_int(n, list_):
  78.     """
  79.    Remains silent unless error detected.
  80.    """
  81.     import gmpy2
  82.     from gmpy2 import is_prime
  83.    
  84.     error_flag = 0
  85.     for e in list_:
  86.         if type(e) != type(4):
  87.             error_flag = 1
  88.             print(c, "is not an integer!")
  89.         elif not is_prime(e):
  90.             error_flag = 1
  91.             print(e, "is not prime!")
  92.     if error_flag == 1:
  93.         print("Not all elements of list of factors are prime! (n = ", n,")", sep='')
  94.     product = 1
  95.     for e in list_:
  96.         product *= e
  97.     if n != product:
  98.         print("Product of factors doesn't equal n! (n = ", n,")", sep='')
  99.            
  100. def factor_random_integers(num_integers, min_length, max_length):
  101.     from time import clock as t
  102.     t0 = t()
  103.     primes = load_pickled_data()
  104.     t1 = t()
  105.     for z in range(num_integers):
  106.         n = randIntOfRandLength(min_length, max_length)
  107.         t2 = t()
  108.         factors = int_factors(n, primes)
  109.         test_of_list_of_prime_factors_of_int(n, factors)
  110.         t3 = t()
  111.         print("Round ", z+1, " of ", num_integers, " (", len(str(n)),
  112.               "-digit integer)", sep='')
  113.         print(n, factors)
  114.         print("factorization time + quality control time was", round((t3-t2),6))
  115.         print()
  116.     print("DONE")
  117.     t11 = t()
  118.     print("Time to load pickled data was", round((t1-t0),2))
  119.     print("Total time for", num_integers, "integers was", round((t11-t0),2))
  120.  
  121. num_integers, min_length, max_length = 10, 8, 16
  122.  
  123. factor_random_integers(num_integers, min_length, max_length)
  124.  
  125. """
  126. EXAMPLE OUTPUT:
  127.  
  128. That f is closed now is True
  129. Round 1 of 10 (15-digit integer)
  130. 301792625554978 [2, 150896312777489]
  131. factorization time + quality control time was 0.201256
  132.  
  133. Round 2 of 10 (13-digit integer)
  134. 7668401194371 [3, 43, 79, 11699, 64319]
  135. factorization time + quality control time was 0.00036
  136.  
  137. Round 3 of 10 (14-digit integer)
  138. 68840270075998 [2, 191, 1571, 114710459]
  139. factorization time + quality control time was 0.000133
  140.  
  141. Round 4 of 10 (16-digit integer)
  142. 3140153631102584 [2, 2, 2, 31, 12661909802833]
  143. factorization time + quality control time was 0.000102
  144.  
  145. Round 5 of 10 (14-digit integer)
  146. 48593145820843 [13, 23741, 157446371]
  147. factorization time + quality control time was 0.0007
  148.  
  149. Round 6 of 10 (15-digit integer)
  150. 751404927012073 [109, 157, 43908427921]
  151. factorization time + quality control time was 0.00011
  152.  
  153. Round 7 of 10 (8-digit integer)
  154. 33691448 [2, 2, 2, 7, 163, 3691]
  155. factorization time + quality control time was 4e-05
  156.  
  157. Round 8 of 10 (12-digit integer)
  158. 653737274053 [31, 21088299163]
  159. factorization time + quality control time was 7.3e-05
  160.  
  161. Round 9 of 10 (13-digit integer)
  162. 5235061190279 [179, 153611, 190391]
  163. factorization time + quality control time was 0.003362
  164.  
  165. Round 10 of 10 (8-digit integer)
  166. 17769484 [2, 2, 19, 229, 1021]
  167. factorization time + quality control time was 4.5e-05
  168.  
  169. DONE
  170. Time to load pickled data was 0.75
  171. Total time for 10 integers was 0.97
  172.  
  173. """
Advertisement
Add Comment
Please, Sign In to add comment