Public Member Functions | |
def | __init__ |
def | type |
def | __eq__ |
def | __ne__ |
def | __cmp__ |
def | has_key |
def | __hash__ |
def | __getitem__ |
def | __delitem__ |
def | __len__ |
def | __mul__ |
def | __div__ |
def | __str__ |
def | asString |
def | asUString |
def | factors |
def | at |
def | add |
def | addString |
def | copy |
def | factor |
def | hasPrime |
def | includes |
def | minimal |
def | nextPrimeFactor |
def | batchPrimeFactor |
def | doPrimeBatch |
def | test_cached_value |
def | first |
def | last |
def | magnitude |
def | matchesFactors |
def | detectPrime |
def | selectLessThan |
def | remove |
def | values |
def | sortedvalues |
def | isPrime |
def | sgmftr |
def | magnitude |
def | trace_view |
def | load_state |
def | save_state |
def | power2mod |
def | isLucasLehmerPrime |
def | pyecm |
def | pollard_rho_factor |
def | pollard_rho_factors_gmpy |
def | print_factors |
def | pollard_rho_factors_python |
def | pollard_rho |
def | egypt_gen |
def | egypt_rho |
def | egypt_rho_inner |
def | egypt_factors |
def | egypt_factor_list |
def | millrab |
def | pollard_brent_factors |
def | pollard_brent |
A dictionary of factors (Number objects). Key is the Number's prime. Value is the Number.
Definition at line 64 of file Factors.py.
def Factors::Factors::__cmp__ | ( | self, | ||
other | ||||
) |
Definition at line 112 of file Factors.py.
00112 : 00113 00114 sn = self._numbers.sortedvalues( ) 00115 sl = len( sn ) 00116 00117 on = other._numbers.sortedvalues( ) 00118 ol = len( on ) 00119 00120 for i in range( 0, min( sl, ol ) ): 00121 s = sn[ i ] 00122 o = on[ i ] 00123 if s != o: 00124 return s.__cmp__(o) 00125 00126 return sl - ol 00127 def has_key( self, key ):
def Factors::Factors::__delitem__ | ( | self, | ||
item | ||||
) |
def Factors::Factors::__div__ | ( | self, | ||
anObject | ||||
) |
Definition at line 193 of file Factors.py.
00193 : 00194 00195 # multiply self by 1 factor or by a factorlist 00196 00197 result = self.copy( ) 00198 00199 if anObject.__class__ != Number.Number: 00200 # ... a factor 00201 for factor in anObject.factors( ): 00202 result = result / factor 00203 else: 00204 # .. a factorlist 00205 00206 factor = anObject 00207 fp = factor.prime( ) 00208 fq = factor.power( ) 00209 00210 if result._numbers.has_key( fp ): 00211 sq = result._numbers[ fp ].power( ) 00212 if sq != fq: 00213 result._numbers[ fp ].power( sq - fq ) 00214 else: 00215 del result._numbers[ fp ] 00216 else: 00217 print "*** Factors.div error ***" 00218 print " num: %s" % result 00219 print " den: %s" % anObject 00220 pydb.debugger( ) 00221 return False 00222 00223 return result 00224 def __str__( self ):
def Factors::Factors::__eq__ | ( | self, | ||
other | ||||
) |
Definition at line 92 of file Factors.py.
00092 : 00093 00094 if other == None: 00095 return False 00096 00097 if len( self._numbers ) != len( other._numbers ): 00098 return False 00099 00100 for key in self._numbers.keys( ): 00101 if not other._numbers.has_key( key ): 00102 return False 00103 if self._numbers[ key ] != other._numbers[ key ]: 00104 return False 00105 00106 return True 00107 def __ne__( self, other ):
def Factors::Factors::__getitem__ | ( | self, | ||
item | ||||
) |
Definition at line 143 of file Factors.py.
00143 : 00144 00145 if item == 0: 00146 pydb.debugger( ) 00147 00148 try: 00149 answer = self._numbers[ item ] 00150 except: 00151 pydb.debugger( ) 00152 answer = self._numbers[ long( item ) ] 00153 00154 return answer 00155 def __delitem__( self, item ):
def Factors::Factors::__hash__ | ( | self | ) |
Definition at line 132 of file Factors.py.
00132 : 00133 00134 v = 1 00135 for n in self._numbers.values( ): 00136 p = n.prime( ) 00137 q = n.power( ) 00138 q = min( q, 10 ) 00139 v = int( ( v * p ** q ) % 1279 ) 00140 00141 return v 00142 def __getitem__( self, item ):
def Factors::Factors::__init__ | ( | self, | ||
n = None | ||||
) |
Definition at line 72 of file Factors.py.
00072 : 00073 00074 self._numbers = AlbDict.AlbDict( ) 00075 self._str = None 00076 self._minimal = None 00077 00078 if not n or n == 1: 00079 return 00080 00081 beg = time.time( ) 00082 self.factor( n ) 00083 fin = time.time( ) 00084 00085 if fin - beg > 60: 00086 globals.Saver.doSave( ) 00087 def type( self ):
def Factors::Factors::__len__ | ( | self | ) |
def Factors::Factors::__mul__ | ( | self, | ||
anObject | ||||
) |
Definition at line 164 of file Factors.py.
00164 : 00165 00166 # multiply self by 1 factor or by a factorlist 00167 00168 result = self.copy( ) 00169 00170 if anObject.__class__ == Number.Number: 00171 # .. a factorlist 00172 factor = anObject 00173 fp = factor.prime( ) 00174 fq = factor.power( ) 00175 00176 if result._numbers.has_key( fp ): 00177 sq = result._numbers[ fp ].power( ) 00178 result._numbers[ fp ].power( sq + fq ) 00179 else: 00180 result.add( factor ) 00181 else: 00182 if anObject.__class__ == Factors: 00183 # ... a factor 00184 factors = anObject.factors( ) 00185 else: # SigmaFactor 00186 factors = anObject.factors( ).factors( ) 00187 00188 for factor in factors: 00189 result.add( factor ) 00190 00191 return result 00192 def __div__( self, anObject ):
def Factors::Factors::__ne__ | ( | self, | ||
other | ||||
) |
def Factors::Factors::__str__ | ( | self | ) |
def Factors::Factors::add | ( | self, | ||
aFactor | ||||
) |
def Factors::Factors::addString | ( | self, | ||
str | ||||
) |
Definition at line 276 of file Factors.py.
00276 : 00277 00278 ( p, q ) = re.split( "[\^]", str ) 00279 p = mpz( p ) 00280 q = mpz( q ) 00281 aFactor = Number.Number( p, q ) 00282 self.add( aFactor ) 00283 def copy( self ):
def Factors::Factors::asString | ( | self | ) |
Definition at line 229 of file Factors.py.
00229 : 00230 00231 if self._str == None: 00232 answer = "" 00233 for f in self.factors( ): 00234 answer += "%s " % f.asString( ) 00235 if not isinstance( self._minimal, types.BooleanType ): 00236 tag = "" 00237 else: 00238 tag = "* " 00239 answer = "( %s%s)" % ( answer, tag ) 00240 self._str = answer 00241 00242 return self._str 00243 def asUString( self ):
def Factors::Factors::asUString | ( | self | ) |
Definition at line 244 of file Factors.py.
00244 : 00245 00246 answer = "( " 00247 for f in self.factors( ): 00248 answer += f.asUString( ) 00249 answer += ")" 00250 00251 return answer 00252 def factors( self ):
def Factors::Factors::at | ( | self, | ||
index | ||||
) |
def Factors::Factors::batchPrimeFactor | ( | self, | ||
val, | ||||
pri = None | ||||
) |
Definition at line 392 of file Factors.py.
00392 : 00393 00394 ans = { } # dict 00395 dis = one_meg 00396 gen = PrimeGenerator.PrimeBatchGenerator( 4 ) 00397 zer = mpz( 0 ) 00398 00399 if not pri: 00400 pri = mpz( 1 ) 00401 else: 00402 # use wheel (skip spokes) 00403 gen.wheel( ) 00404 00405 tim = time.time( ) 00406 00407 self._trace_cnt = 0 00408 self._next_trace = one_min 00409 00410 while val > 1 and pri * pri <= val: 00411 00412 ( val, pri ) = self.doPrimeBatch( val, ans, gen, pri, zer ) 00413 00414 if pri > dis: 00415 ( dis, tim ) = self.trace_view( tim, val, pri, dis ) 00416 00417 if val > 1: 00418 ans[ val ] = Number.Number( val, 1 ) 00419 00420 return ans 00421 def doPrimeBatch( self, v, r, g, p, z ):
def Factors::Factors::copy | ( | self | ) |
Definition at line 284 of file Factors.py.
00284 : 00285 00286 # answer a copy of self 00287 00288 new = Factors( None ) 00289 for number in self._numbers.values( ): 00290 new.add( number.copy( ) ) 00291 00292 return new 00293 def factor( self, anInteger ):
def Factors::Factors::detectPrime | ( | self, | ||
p | ||||
) |
Definition at line 499 of file Factors.py.
00499 : 00500 00501 if self._numbers.has_key( p ): 00502 return True 00503 else: 00504 return False 00505 def selectLessThan( self, rdr ):
def Factors::Factors::doPrimeBatch | ( | self, | ||
v, | ||||
r, | ||||
g, | ||||
p, | ||||
z | ||||
) |
Definition at line 422 of file Factors.py.
00422 : 00423 00424 primes = g.primes( ) 00425 00426 for d in primes: 00427 p += d 00428 00429 if v % p != z: 00430 continue 00431 q = 0 00432 while v % p == 0: 00433 q += 1 00434 v = v / p 00435 00436 r[ p ] = Number.Number( p, q ) 00437 if q != 0 and v > one_meg: 00438 if self.test_cached_value( v, r ): 00439 v = 1 00440 break 00441 00442 return ( v, p ) 00443 def test_cached_value( self, v, r ):
def Factors::Factors::egypt_factor_list | ( | self, | ||
n | ||||
) |
Return a list of the factors of n. Uses trial division for small primes before switching to Pollard's Rho method.
Definition at line 861 of file Factors.py.
00861 : 00862 """ 00863 Return a list of the factors of n. 00864 Uses trial division for small primes before switching to Pollard's Rho method. 00865 """ 00866 f = [ ] 00867 for p in [ 2, 3, 5, 7, 11, 13, 17, 19 ]: 00868 q = 0 00869 while n % p == 0: 00870 n //= p 00871 q += 1 00872 if q != 0: 00873 f.append( p ) 00874 if n == 1: 00875 return f 00876 if self.millrab( n ): 00877 return f + [n] 00878 maxt, maxc = 100, 5 00879 rho_results = [ n ] 00880 r = 1 00881 while len( rho_results ) == 1: 00882 beg = time.time( ) 00883 rho_results = self.egypt_rho( n, maxt, maxc) 00884 fin = time.time( ) 00885 if globals.quiet <= 1: 00886 print "n %s, r %d, t %5.2f" % ( n, r, fin - beg ) 00887 maxt *= 2 00888 maxc *= 2 00889 r = r * 2 00890 if globals.quiet <= 1: 00891 print rho_results 00892 for factor in rho_results: 00893 f += self.egypt_factor_list( factor ) 00894 return f 00895 def millrab( self, n, max=30 ):
def Factors::Factors::egypt_factors | ( | self, | ||
n | ||||
) |
Definition at line 852 of file Factors.py.
00852 : 00853 factors = self.egypt_factor_list( n ) 00854 result = { } 00855 for f in factors: 00856 result[ f ] = result.get( f, 0 ) + 1 00857 for r in result.keys( ): 00858 result[ r ] = Number.Number( r, result[ r ] ) 00859 return result 00860 def egypt_factor_list( self, n ):
def Factors::Factors::egypt_gen | ( | self, | ||
n, | ||||
c = 1 | ||||
) |
Generate sequence x_i = (x_{i-1}^2 + c) mod n Where n is the target composite we want to factor.
Definition at line 796 of file Factors.py.
00796 : 00797 """ 00798 Generate sequence x_i = (x_{i-1}^2 + c) mod n 00799 Where n is the target composite we want to factor. 00800 """ 00801 x = 1 00802 while True: 00803 x = ( x ** 2 + c ) % n 00804 yield x 00805 def egypt_rho( self, n, maxt=500, maxc=10 ):
def Factors::Factors::egypt_rho | ( | self, | ||
n, | ||||
maxt = 500 , |
||||
maxc = 10 | ||||
) |
Pollard's Rho method for factoring n. Returns a list of factors (not necessarily prime) of n. Tests each polynomial x^2+c (c in range(1,maxc)) by following the sequence gen(n,c) for maxt steps. If the sequence is cyclic modulo a factor of n with a smaller cycle length than its cycle modulo n, we can identify a factor of n as the gcd of n with the difference of two sequence values separated by the smaller cycle length in the sequence.
Definition at line 806 of file Factors.py.
00806 : 00807 """ 00808 Pollard's Rho method for factoring n. 00809 Returns a list of factors (not necessarily prime) of n. 00810 Tests each polynomial x^2+c (c in range(1,maxc)) 00811 by following the sequence gen(n,c) for maxt steps. 00812 If the sequence is cyclic modulo a factor of n 00813 with a smaller cycle length than its cycle modulo n, 00814 we can identify a factor of n as the gcd of n 00815 with the difference of two sequence values separated 00816 by the smaller cycle length in the sequence. 00817 """ 00818 # if millrab(n): # don't bother with probable primes 00819 # return [n] 00820 00821 maxt = min( maxt, n ) 00822 for c in range( 1, maxc ): 00823 results = self.egypt_rho_inner( n, c, maxt, maxc ) 00824 if results and len( results ) != 1: 00825 return results 00826 00827 return [ n ] # failure to factor 00828 def egypt_rho_inner( self, n, c, maxt, maxc ):
def Factors::Factors::egypt_rho_inner | ( | self, | ||
n, | ||||
c, | ||||
maxt, | ||||
maxc | ||||
) |
Definition at line 829 of file Factors.py.
00829 : 00830 seqslow = self.egypt_gen( n, c ) 00831 seqfast = self.egypt_gen( n, c ) 00832 for trial in range( maxt ): 00833 xb = seqslow.next( ) # slow generator goes one step 00834 seqfast.next( ) 00835 xk = seqfast.next( ) # while fast generator goes two 00836 diff = abs( xk-xb ) 00837 if not diff: 00838 if globals.quiet <= 1: 00839 print "n -> %d" % n 00840 return None # [ n ] 00841 # continue 00842 d = self.egypt_gcd( diff,n ) # have a factor? 00843 if n > d > 1: 00844 if globals.quiet <= 1: 00845 print "%s -> ( %s %s )" % ( n, d, n / d ) 00846 return [ d, n // d ] 00847 return [ n ] 00848 00849 # Find all factors and divisors 00850 # Fix rho (doesn't handle powers of two correctly) and speed up small primes 00851 def egypt_factors( self, n ):
def Factors::Factors::factor | ( | self, | ||
anInteger | ||||
) |
Definition at line 294 of file Factors.py.
00294 : 00295 00296 # Return a dictionary with the p:q factors of anInteger 00297 00298 v = mpz( anInteger ) 00299 00300 if v > one_meg: 00301 factors = globals.FactorCache.at( v ) 00302 00303 if factors != None: 00304 self._numbers = factors._numbers.copy( ) 00305 return 00306 00307 if v > ten_gig and not globals.quiet: 00308 print "factoring: %s" % v, 00309 sys.stdout.flush() 00310 00311 if v < ten_pow_15: 00312 r = self.batchPrimeFactor( v ) 00313 else: 00314 # r = self.pollard_rho_factor( v ) 00315 r = self.pyecm( v ) 00316 globals.Saver.doSave( True ) 00317 00318 self._numbers = r 00319 00320 if anInteger > one_meg: 00321 if not globals.quiet: 00322 print 00323 print "%s" % self 00324 sys.stdout.flush() 00325 globals.FactorCache.add( anInteger, self ) 00326 00327 return 00328 def hasPrime( self, prime ):
def Factors::Factors::factors | ( | self | ) |
Definition at line 253 of file Factors.py.
00253 : 00254 # return a sorted list of p,q pairs 00255 00256 answer = self._numbers.values( ) 00257 if len( answer ) > 0: 00258 answer.sort( ) 00259 00260 return answer 00261 def at( self, index ):
def Factors::Factors::first | ( | self | ) |
def Factors::Factors::has_key | ( | self, | ||
key | ||||
) |
def Factors::Factors::hasPrime | ( | self, | ||
prime | ||||
) |
def Factors::Factors::includes | ( | self, | ||
factors | ||||
) |
Definition at line 333 of file Factors.py.
00333 : 00334 00335 # pydb.debugger( ) 00336 for factor in factors.values( ): 00337 prime = factor.prime( ) 00338 if not self.has_key( prime ): 00339 return False 00340 if self[ prime ] != factor: 00341 return False 00342 00343 return True 00344 def minimal( self, bool ):
def Factors::Factors::isLucasLehmerPrime | ( | self, | ||
q | ||||
) |
Definition at line 635 of file Factors.py.
00635 : 00636 00637 # http://primes.utm.edu/mersenne/index.html#test 00638 00639 # Lucas-Lehmer Test: For p an odd prime, the Mersenne number 2^p-1 is 00640 # prime if and only if 2^p-1 divides S(p-1) where S(n+1) = S(n)^2-2, 00641 # and S(1) = 4. [Proof.] 00642 00643 if not Factors( q ).isPrime( ): 00644 return False 00645 00646 beg = time.time( ) 00647 msg = None 00648 00649 v = mpz( 2 ) ** q - 1 00650 s = mpz( 4 ) 00651 00652 for i in range( 2, q ): 00653 s = s ** 2 - 2 00654 # s = s % v 00655 s = self.power2mod( s, v, q ) 00656 fin = time.time( ) 00657 00658 if s != 0: 00659 result = 0 00660 if verbose > 1: 00661 msg = "--" 00662 else: 00663 result = v 00664 if verbose > 0: 00665 msg = "mp" 00666 00667 if msg != None: 00668 tim = TimeStamp.TimeStamp( "p" ) 00669 print " %s %8.2f %5d %s" % ( tim, fin - beg, q, msg ) 00670 sys.stdout.flush() 00671 00672 return result 00673 def pyecm( self, value ):
def Factors::Factors::isPrime | ( | self | ) |
def Factors::Factors::last | ( | self | ) |
def Factors::Factors::load_state | ( | self | ) |
Definition at line 608 of file Factors.py.
00608 : 00609 00610 file = open( state_file, "r" ) 00611 line = file.readline( ) 00612 file.close( ) 00613 00614 ( val, pri ) = re.split( "[ ,]+", line ) 00615 00616 return ( val, pri ) 00617 def save_state( self, val, pri ):
def Factors::Factors::magnitude | ( | self | ) |
Definition at line 550 of file Factors.py.
00550 : 00551 00552 answer = 1 00553 for factor in self.sortedvalues( ): 00554 try: 00555 magnitude = factor.magnitude( ) 00556 except: 00557 pydb.debugger( ) 00558 magnitude = factor.magnitude( ) 00559 answer *= magnitude 00560 00561 return answer 00562 def trace_view( self, tim, val, pri, dis ):
def Factors::Factors::magnitude | ( | self | ) |
Definition at line 478 of file Factors.py.
00478 : 00479 v = 1 00480 for n in self._numbers.values( ): 00481 m = n.magnitude( ) 00482 v = v * m 00483 00484 return v 00485 def matchesFactors( self, factors ):
def Factors::Factors::matchesFactors | ( | self, | ||
factors | ||||
) |
Definition at line 486 of file Factors.py.
00486 : 00487 00488 for factor in factors.values( ): 00489 p = factor.prime( ) 00490 try: 00491 value = self._numbers[ p ] 00492 if value.power( ) != factor.power( ): 00493 return False 00494 except: 00495 return False 00496 00497 return True 00498 def detectPrime( self, p ):
def Factors::Factors::millrab | ( | self, | ||
n, | ||||
max = 30 | ||||
) |
Miller-Rabin primality test as per the following source: http://www.wikipedia.org/wiki/Miller-Rabin_primality_test Returns probability p is prime: either p = 0 or ~1,
Definition at line 896 of file Factors.py.
00896 : 00897 """ 00898 Miller-Rabin primality test as per the following source: 00899 http://www.wikipedia.org/wiki/Miller-Rabin_primality_test 00900 Returns probability p is prime: either p = 0 or ~1, 00901 """ 00902 if not n % 2: return 0 00903 k = 0 00904 z = n - 1 00905 00906 # compute m,k such that (2**k)*m = n-1 00907 while not z % 2: 00908 k += 1 00909 z //= 2 00910 m = z 00911 00912 # try tests with max random integers between 2,n-1 00913 ok = 1 00914 trials = 0 00915 p = 1 00916 while trials < max and ok: 00917 a = randrange( 2, long( n ) - 1 ) 00918 trials += 1 00919 test = pow( a, m, n ) 00920 if ( not test == 1 ) and not ( test == n - 1 ): 00921 # if 1st test fails, fall through 00922 ok = 0 00923 for r in range( 1, k ): 00924 test = pow( a, ( 2 ** r )* m, n ) 00925 if test == ( n - 1 ): 00926 ok = 1 # 2nd test ok 00927 break 00928 else: ok = 1 # 1st test ok 00929 if ok == 1: p *= 0.25 00930 00931 if ok: return 1 - p 00932 else: return 0 00933 00934 00935 # http://en.wikipedia.org/wiki/Pollard_rho 00936 # Richard Brent's variant 00937 def pollard_brent_factors( self, value ):
def Factors::Factors::minimal | ( | self, | ||
bool | ||||
) |
Definition at line 345 of file Factors.py.
00345 : 00346 00347 self._minimal = bool 00348 self._str = None 00349 00350 return 00351 def nextPrimeFactor( self, val ):
def Factors::Factors::nextPrimeFactor | ( | self, | ||
val | ||||
) |
Definition at line 352 of file Factors.py.
00352 : 00353 00354 gen = PrimeGenerator.PrimeGenerator( 3 ) 00355 00356 pri = mpz( 1 ) 00357 zer = mpz( 0 ) 00358 dis = one_meg 00359 00360 ans = { } # dict 00361 00362 tim = time.time( ) 00363 00364 self._trace_cnt = 0 00365 00366 while True: 00367 pri = gen.NextPrime( ) 00368 00369 if pri * pri > val: 00370 break 00371 00372 if val % pri == zer: 00373 exp = 0 00374 while val % pri == 0: 00375 exp += 1 00376 val = val / pri 00377 00378 ans[ pri ] = Number.Number( pri, exp ) 00379 00380 if v > one_meg: 00381 if self.test_cached_value( val, ans ): 00382 break 00383 00384 if pri > dis: 00385 ( dis, tim ) = self.trace_view( tim, val, pri, dis ) 00386 00387 if val > 1: 00388 ans[ val ] = Number.Number( val, 1 ) 00389 00390 return ans 00391 def batchPrimeFactor( self, val, pri = None ):
def Factors::Factors::pollard_brent | ( | self, | ||
value | ||||
) |
Definition at line 952 of file Factors.py.
00952 : 00953 n = value 00954 m = 1 00955 #####1. y = x0, r = 1, q = 1.
def Factors::Factors::pollard_brent_factors | ( | self, | ||
value | ||||
) |
Definition at line 938 of file Factors.py.
00938 : 00939 ans = { } 00940 while ( value > 1 ): 00941 f = self.pollard_brent( value ) 00942 if not f or f == 1: 00943 f = value 00944 q = 0 00945 while ( value % f ) == 0: 00946 q += 1 00947 value = value / f 00948 ans[ f ] = Number.Number( f, q ) 00949 00950 return ans 00951 def pollard_brent( self, value ):
def Factors::Factors::pollard_rho | ( | self, | ||
starts, | ||||
value | ||||
) |
factorPR(n) - Find a factor of n using the Pollard Rho method. Note: This method will occasionally fail.
Definition at line 757 of file Factors.py.
00757 : 00758 00759 """ 00760 factorPR(n) - Find a factor of n using the Pollard Rho method. 00761 Note: This method will occasionally fail. 00762 """ 00763 # http://www.math.umbc.edu/~campbell/Computers/Python/numbthy.py 00764 00765 one = mpz( 1 ) 00766 intrvl = one_meg 00767 for start in starts: 00768 slow = mpz( start ) 00769 numsteps = 2 * math.floor( math.sqrt( math.sqrt( value ) ) ) 00770 numsteps += 1000 00771 numsteps = mpz( numsteps ) 00772 fast = slow 00773 i = one 00774 d = intrvl 00775 while i < numsteps: 00776 if i == d: 00777 d = self.rho_status_display( value, numsteps, start, i, intrvl ) 00778 00779 slow = ( slow * slow + one ) % value 00780 i = i + one 00781 fast = ( fast * fast + one ) % value 00782 fast = ( fast * fast + one ) % value 00783 g = self.gcd( max( fast, slow ) - min( fast, slow ), value ) 00784 if ( g != one ): 00785 if ( g == value ): 00786 # print "g == value **** break" 00787 break 00788 else: 00789 if g > one_meg or start != 2: 00790 print TimeStamp.TimeStamp( ), 00791 print "*** pollard_rho_factor: s %d, g %d" % ( start, g ) 00792 return g 00793 # print "-> 1" 00794 return 1 00795 def egypt_gen( self, n, c=1 ):
def Factors::Factors::pollard_rho_factor | ( | self, | ||
value | ||||
) |
Definition at line 693 of file Factors.py.
00693 : 00694 return self.pollard_rho_factors_gmpy( value ) 00695 def pollard_rho_factors_gmpy( self, value ):
def Factors::Factors::pollard_rho_factors_gmpy | ( | self, | ||
value | ||||
) |
Definition at line 696 of file Factors.py.
00696 : 00697 00698 fvalue = value * 1.0 00699 if fvalue > 1.0e20: 00700 print "pollard_rho_factors_gmpy: %e %s" % ( fvalue, value ) 00701 sys.stdout.flush() 00702 00703 # pysymbolicext.verbose( ); 00704 beg = time.time( ) 00705 00706 factors = pysymbolicext.factor( value ) 00707 ans = { } # dict 00708 for factor in factors: 00709 ( p, q ) = factor 00710 if ans.has_key( p ): 00711 q += ans[ p ].power( ) 00712 ans[ p ] = Number.Number( p, q ) 00713 00714 if fvalue > 1.0e20: 00715 print "%s" % ans 00716 sys.stdout.flush() 00717 00718 fin = time.time( ) 00719 tim = TimeStamp.TimeStamp( "p" ) 00720 print " %s %8.2f %s" % ( tim, fin - beg, ans ) 00721 00722 return ans 00723 def print_factors( self, value, factors, tim = 0 ):
def Factors::Factors::pollard_rho_factors_python | ( | self, | ||
value | ||||
) |
Definition at line 738 of file Factors.py.
00738 : 00739 00740 # print "pollard_rho_factors_python %s" % value 00741 ans = { } 00742 starts = [ 2, 3, 4, 6 ] 00743 while ( value > 1 ): 00744 f = self.pollard_rho( starts, value ) 00745 if f == 1: 00746 f = value 00747 q = 0 00748 print value, f, value / f 00749 pydb.debugger( ) 00750 while ( value % f ) == 0: 00751 q += 1 00752 value = value / f 00753 ans[ f ] = Number.Number( f, q ) 00754 00755 return ans 00756 def pollard_rho( self, starts, value ):
def Factors::Factors::power2mod | ( | self, | ||
val, | ||||
mer, | ||||
exp | ||||
) |
Definition at line 626 of file Factors.py.
00626 : 00627 rslt = ( val & mer ) + ( val >> exp ) 00628 if rslt >= mer: 00629 rslt -= mer 00630 if rslt >= mer: 00631 rslt -= mer 00632 print "power2mod: %d %d %d" % ( val, mer, exp ) 00633 return rslt 00634 def isLucasLehmerPrime( self, q ):
def Factors::Factors::print_factors | ( | self, | ||
value, | ||||
factors, | ||||
tim = 0 | ||||
) |
Definition at line 724 of file Factors.py.
00724 : 00725 00726 # print factors dictionary 00727 00728 print "%s (" % value, 00729 keys = factors.keys( ) 00730 keys.sort( ) 00731 for key in keys: 00732 print "%s" % factors[ key ], 00733 if tim < 0.1: 00734 print ")" 00735 else: 00736 print ")", " *** %5.2f secs" % tim 00737 def pollard_rho_factors_python( self, value ):
def Factors::Factors::pyecm | ( | self, | ||
value | ||||
) |
Definition at line 674 of file Factors.py.
00674 : 00675 00676 fvalue = value * 1.0 00677 if fvalue > 1.0e20: 00678 print "pyecm: %e %s" % ( fvalue, value ) 00679 sys.stdout.flush() 00680 beg = time.time( ) 00681 00682 ans = pyecm_wrapper.factors_as_dict_of_numbers( value ) 00683 if fvalue > 1.0e20: 00684 print "pyecm: %e %s" % ( fvalue, value ) 00685 sys.stdout.flush() 00686 00687 fin = time.time( ) 00688 tim = TimeStamp.TimeStamp( "p" ) 00689 print " %s %8.2f %s" % ( tim, fin - beg, ans ) 00690 00691 return ans 00692 def pollard_rho_factor( self, value ):
def Factors::Factors::remove | ( | self, | ||
p | ||||
) |
def Factors::Factors::save_state | ( | self, | ||
val, | ||||
pri | ||||
) |
Definition at line 618 of file Factors.py.
00618 : 00619 00620 if pri > one_gig: 00621 name = "%s.txt" % val 00622 file = open( name, "w" ) 00623 file.write( "%s %s\n" % ( val, pri ) ) 00624 file.close( ) 00625 def power2mod( self, val, mer, exp ):
def Factors::Factors::selectLessThan | ( | self, | ||
rdr | ||||
) |
Definition at line 506 of file Factors.py.
00506 : 00507 00508 result = Factors( None ) 00509 for factor in self._numbers.values( ): 00510 if factor < rdr: 00511 result.add( factor ) 00512 00513 return result 00514 def remove( self, p ):
def Factors::Factors::sgmftr | ( | self | ) |
Definition at line 540 of file Factors.py.
00540 : 00541 00542 answer = Factors( ) 00543 values = self.sortedvalues( ) 00544 for factor in values: 00545 sgmftr = factor.sgmftr( ) 00546 answer *= sgmftr 00547 00548 return answer 00549 def magnitude( self ):
def Factors::Factors::sortedvalues | ( | self | ) |
def Factors::Factors::test_cached_value | ( | self, | ||
v, | ||||
r | ||||
) |
Definition at line 444 of file Factors.py.
00444 : 00445 00446 factors = globals.FactorCache.at( v ) 00447 if not factors: 00448 return False 00449 00450 for factor in factors.values( ): 00451 p = factor.prime( ) 00452 if r.has_key( p ): 00453 print "**********" 00454 print "rpt: %s" % p 00455 print "**********" 00456 sys.stdout.flush() 00457 pydb.debugger( ) 00458 00459 r[ p ] = factor.copy( ) 00460 00461 return True 00462 def first( self ):
def Factors::Factors::trace_view | ( | self, | ||
tim, | ||||
val, | ||||
pri, | ||||
dis | ||||
) |
Definition at line 563 of file Factors.py.
00563 : 00564 00565 cur = time.time( ) 00566 if cur > tim and globals.quiet <= 2: 00567 00568 self.save_state( val, pri ) 00569 00570 pri = pri * 1.0 00571 sqrt = mpz( val ** 0.50 ) 00572 00573 tim = "%s" % TimeStamp.TimeStamp( ) 00574 00575 if dis == one_meg: 00576 print "%s %d %8.3e %d %8.3e" % ( tim, long( val ), val, long( sqrt ), sqrt ) 00577 tim = " " * ( len ( tim ) - 5 ) 00578 str = "%8.2f Mb" % ( pri / one_meg ) 00579 lbl = "Mb" 00580 div = one_meg 00581 else: 00582 lbl = "Gb" 00583 div = one_gig 00584 str = "%8.2f Gb" % ( pri / one_gig ) 00585 fmt = " %s %s %8.2f %7.2f" 00586 print fmt % ( tim, str, val / pri / pri, pri / sqrt * 100.0 ) 00587 # fmt = "%s %8.2f %s %8.2f %7.2f" 00588 # print fmt % ( tim, pri / div, lbl, val / pri / pri, pri / sqrt * 100.0 ) 00589 sys.stdout.flush( ) 00590 00591 self._trace_cnt += 1 00592 if self._trace_cnt == 5: 00593 self._next_trace = one_hour 00594 00595 tim = cur + self._next_trace 00596 00597 if not globals.quiet: 00598 if dis == one_meg: 00599 print "factoring: %s" % val 00600 print ".", 00601 sys.stdout.flush() 00602 globals.Saver.doSave( ) 00603 00604 dis += one_meg 00605 00606 return ( dis, tim ) 00607 def load_state( self ):
def Factors::Factors::type | ( | self | ) |
def Factors::Factors::values | ( | self | ) |
Definition at line 520 of file Factors.py.
00520 : 00521 00522 return self._numbers.values( ) 00523 def sortedvalues( self ):