pyzuki

function: factor_integer()

Aug 22nd, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. # function: factor_integer()
  2.  
  3. def factor_integer(n):
  4.     """
  5.    prints the integer and the list of its factors
  6.    
  7.    E.g.
  8.    >>> factor_integer(198273646782734)
  9.    198,273,646,782,734 [2, 17, 2243, 2599900957]
  10.    
  11.    Employs a data file of primes to 100 million (5,761,459 primes),
  12.       thereby ensuring quick factorization of any integer up to
  13.       10 quadrillion (1e17).
  14.      
  15.    This data file may be downloaded from
  16.       http://www.rcblue.com/Misc2/primes_to_100_million.dat
  17.      
  18.    gmpy2 is available at http://code.google.com/p/gmpy/
  19.    """
  20.     import pickle
  21.     import os.path
  22.     import gmpy2
  23.     from gmpy2 import is_prime
  24.    
  25.     def int_commas(n):
  26.         """
  27.        inserts commas into integers and returns the string
  28.    
  29.        E.g. -12345678 -> -12,345,789
  30.        """
  31.         return format(n, ',d')    
  32.    
  33.     def test_of_list_of_prime_factors_of_int(n, list_):
  34.         """
  35.        Remains silent unless error detected.
  36.        """
  37.         error_flag = 0
  38.         for e in list_:
  39.             if not is_prime(e):
  40.                 error_flag = 1
  41.                 print(e, "is not prime!")
  42.         if error_flag == 1:
  43.             print("Not all elements of list of factors are prime! (n = ", n,")", sep='')
  44.         product = 1
  45.         for e in list_:
  46.             product *= e
  47.         if n != product:
  48.             print("Product of factors doesn't equal n! (n = ", n,")", sep='')
  49.    
  50.     def load_pickled_data(path):
  51.            
  52.         if os.path.getsize(path) == 0:
  53.             #The file exists, but is empty: leave it alone
  54.             pass
  55.        
  56.         elif os.path.exists(path):
  57.             #The file exists and is not empty: use it
  58.             with open(path, "rb") as f:
  59.                 data = pickle.load(f)
  60.        
  61.         D = data
  62.         primes = D
  63.         return primes    
  64.    
  65.     def int_factors(n, primes):
  66.         factors = []
  67.         #limit = int(n**.5) + 1
  68.        
  69.         if is_prime(n):
  70.             return [int(n)]
  71.    
  72.         if n == 1:
  73.             return []
  74.        
  75.         for x in primes:
  76.             while n % x == 0:
  77.                 factors.append(int(x))
  78.                 n = n // x
  79.                 if is_prime(n):
  80.                     factors.append(int(n))
  81.                     return factors
  82.         while True:
  83.            
  84.             x += 2
  85.            
  86.             while n % x == 0:
  87.                 factors.append(int(x))
  88.                 n = n // x
  89.                 if is_prime(n):
  90.                     factors.append(n)
  91.                     return factors
  92.        
  93.     path = 'C:\P32Working\Data\primes_to_100_million.dat'
  94.     primes = load_pickled_data(path)
  95.     factors = int_factors(n, primes)
  96.     test_of_list_of_prime_factors_of_int(n, factors)
  97.     print(int_commas(n), factors)
  98.    
  99. print(factor_integer(94583726378))
  100.  
  101. """
  102. OUTPUT:
  103. 94,583,726,378 [2, 19, 2489045431]
  104. """
Advertisement
Add Comment
Please, Sign In to add comment