def getECMFactors( target ): from pyecm import factors n = int( floor( target ) ) verbose = g.verbose randomSigma = True asymptoticSpeed = 10 processingPower = 1.0 if n < -1: return [ ( -1, 1 ) ] + getECMFactors( fneg( n ) ) elif n == -1: return [ ( -1, 1 ) ] elif n == 0: return [ ( 0, 1 ) ] elif n == 1: return [ ( 1, 1 ) ] if verbose: print( '\nfactoring', n, '(', int( floor( log10( n ) ) ), ' digits)...' ) if g.factorCache is None: loadFactorCache( ) if n in g.factorCache: if verbose and n != 1: print( 'cache hit:', n ) print( ) return g.factorCache[ n ] result = [ ] for factor in factors( n, verbose, randomSigma, asymptoticSpeed, processingPower ): result.append( factor ) result = [ int( i ) for i in result ] largeFactors = list( collections.Counter( [ i for i in result if i > 65535 ] ).items( ) ) product = int( fprod( [ power( i[ 0 ], i[ 1 ] ) for i in largeFactors ] ) ) save = False if product not in g.factorCache: g.factorCache[ product ] = largeFactors save = True result = list( collections.Counter( result ).items( ) ) if n > g.minValueToCache and n not in g.factorCache: g.factorCache[ n ] = result g.factorCacheIsDirty = True if verbose: print( ) return result
def getFactors( n ): ''' A factorization of *Nptr into prime and q-prime factors is first obtained. Selfridge's primality test is then applied to any q-prime factors; the test is applied repeatedly until either a q-prime is found to be composite or likely to be composite (in which case the initial factorization is doubtful and an extra base should be used in Miller's test) or we run out of q-primes, in which case every q-prime factor of *Nptr is certified as prime. Returns a list of tuples where each tuple is a prime factor and an exponent. ''' verbose = g.verbose if real( n ) < -1: return [ ( -1, 1 ) ] + getFactors( fneg( n ) ) elif n == -1: return [ ( -1, 1 ) ] elif n == 0: return [ ( 0, 1 ) ] elif n == 1: return [ ( 1, 1 ) ] target = int( n ) dps = ceil( log( n ) ) if dps > mp.dps: setAccuracy( dps ) if target > g.minValueToCache: if g.factorCache is None: loadFactorCache( ) if target in g.factorCache: if verbose: print( 'cache hit:', target ) return g.factorCache[ target ] smallFactors, largeFactors, qPrimes = getPrimeFactors( int( n ), verbose ) if qPrimes: if verbose: print( 'testing q-primes for primality' ) print( '--' ) i = 0 for qPrime in qPrimes: t = doSelfridgeTest( qPrime[ 0 ], verbose ) if not t: print( 'do FACTOR() again with an extra base' ) return 0 if verbose: print( 'all q-primes are primes:', n, 'has the following factorization:' ) print( ) for i in smallFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) for i in largeFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) else: if verbose: print( 'NO Q-PRIMES:' ) print( ) print( n, 'has the following factorization:' ) for i in smallFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) for i in largeFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) result = [ ] result.extend( smallFactors ) result.extend( largeFactors ) if g.factorCache is not None: product = int( fprod( [ power( i[ 0 ], i[ 1 ] ) for i in largeFactors ] ) ) if product not in g.factorCache: g.factorCache[ product ] = largeFactors g.factorCacheIsDirty = True if n > g.minValueToCache and n not in g.factorCache: g.factorCache[ n ] = result g.factorCacheIsDirty = True return result
def getFactors( n ): verbose = g.verbose if real( n ) < -1: return [ ( -1, 1 ) ] + getFactors( fneg( n ) ) elif n == -1: return [ ( -1, 1 ) ] elif n == 0: return [ ( 0, 1 ) ] elif n == 1: return [ ( 1, 1 ) ] target = int( n ) dps = ceil( log( n ) ) if dps > mp.dps: setAccuracy( dps ) if target > g.minValueToCache: if g.factorCache is None: loadFactorCache( ) if target in g.factorCache: if verbose: print( 'cache hit:', target ) return g.factorCache[ target ] smallFactors, largeFactors, qPrimes = getPrimeFactors( int( n ), verbose ) if qPrimes: if verbose: print( 'testing q-primes for primality' ) print( '--' ) i = 0 for qPrime in qPrimes: t = doSelfridgeTest( qPrime[ 0 ], verbose ) if not t: print( 'do FACTOR() again with an extra base' ) return 0 if verbose: print( 'all q-primes are primes:', n, 'has the following factorization:' ) print( ) for i in smallFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) for i in largeFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) else: if verbose: print( 'NO Q-PRIMES:' ) print( ) print( n, 'has the following factorization:' ) for i in smallFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) for i in largeFactors: print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] ) result = [ ] result.extend( smallFactors ) result.extend( largeFactors ) if g.factorCache is not None: product = int( fprod( [ power( i[ 0 ], i[ 1 ] ) for i in largeFactors ] ) ) if product not in g.factorCache: g.factorCache[ product ] = largeFactors g.factorCacheIsDirty = True if n > g.minValueToCache and n not in g.factorCache: g.factorCache[ n ] = result g.factorCacheIsDirty = True return result