Changeset 7801:3796f307c986


Ignore:
Timestamp:
12/07/07 11:26:17 (5 years ago)
Author:
Paul Zimmermann <zimmerma@…>
Branch:
default
Message:

new function one_curve to call one ECM/P-1/P+1 curve
added examples for find_factor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/interfaces/ecm.py

    r7360 r7801  
    150150 
    151151     
     152    def one_curve(self, n, factor_digits=None, B1=2000, method="ECM", **kwds): 
     153        """ 
     154        Run one single ECM (or P-1/P+1) curve on input n. 
     155        INPUT: 
     156            n -- a positive integer 
     157            factor_digits -- decimal digits estimate of the wanted factor 
     158            B1 -- stage 1 bound (default 2000) 
     159            method -- either "ECM" (default), "P-1" or "P+1" 
     160        OUTPUT: 
     161            a list [p,q] where p and q are integers and n = p * q. 
     162            If no factor was found, then p = 1 and q = n. 
     163            WARNING: neither p nor q is guaranteed to be prime. 
     164        EXAMPLES: 
     165            sage: f = ECM() 
     166            sage: n = 508021860739623467191080372196682785441177798407961 
     167            sage: f.one_curve(n, B1=10000, sigma=11) 
     168            [1, 508021860739623467191080372196682785441177798407961] 
     169            sage: f.one_curve(n, B1=10000, sigma=1022170541) 
     170            [79792266297612017, 6366805760909027985741435139224233] 
     171            sage: n = 432132887883903108009802143314445113500016816977037257 
     172            sage: f.one_curve(n, B1=500000, method="P-1") 
     173            [67872792749091946529, 6366805760909027985741435139224233] 
     174            sage: n = 2088352670731726262548647919416588631875815083 
     175            sage: f.one_curve(n, B1=2000, method="P+1", x0=5) 
     176            [328006342451, 6366805760909027985741435139224233] 
     177        """ 
     178        if not factor_digits is None:  
     179            B1 = self.recommended_B1(factor_digits) 
     180        if method == "P-1": 
     181            kwds['pm1'] = '' 
     182        elif method == "P+1": 
     183            kwds['pp1'] = '' 
     184        else: 
     185           if not method == "ECM": 
     186              err = "unexpected method: " + method 
     187              raise ValueError, method 
     188        self.__cmd = self._ECM__startup_cmd(B1, None, kwds) 
     189        child = pexpect.spawn(self.__cmd)         
     190        cleaner.cleaner(child.pid, self.__cmd) 
     191        child.timeout = None 
     192        child.__del__ = nothing   # work around stupid exception ignored error 
     193        child.expect('[ECM]') 
     194        child.sendline(str(n)) 
     195        child.sendline("bad") # child.sendeof() 
     196        while True: 
     197            try:  
     198               child.expect('(Using B1=(\d+), B2=(\d+), polynomial ([^,]+), sigma=(\d+)\D)|(Factor found in step \d:\s+(\d+)\D)|(Error - invalid number)') 
     199               info = child.match.groups() 
     200               # B1 is info[1], B2 is info[2], poly is info[3], sigma is info[4], 
     201               # step is info[5], factor is info[6], cofactor is info[7] 
     202               if not info[0] is None: 
     203                  # got Using B1=... line 
     204                  self.last_params = { 'B1' : child.match.groups()[1],  
     205                                       'B2' : child.match.groups()[2],  
     206                                       'poly' : child.match.groups()[3],  
     207                                       'sigma' : child.match.groups()[4] } 
     208               elif info[7] != None: 
     209                  # got Error - invalid number, which means the curve did 
     210                  # end without finding any factor, and the next input 'bad' 
     211                  # was given to GMP-ECM 
     212                  child.kill(0) 
     213                  return [1, n] 
     214               else: 
     215                  # got Factor found... 
     216                  p = Integer(info[6]) 
     217                  child.kill(0) 
     218                  return [p, n/p] 
     219 
     220            except pexpect.EOF: 
     221               child.kill(0) 
     222               return [1, n] 
     223            child.kill(0) 
     224 
     225 
    152226    def find_factor(self, n, factor_digits=None, B1=2000, **kwds): 
    153227        """ 
    154228        Splits off a single factor of n. 
    155229        See ECM.factor() 
     230        EXAMPLES: 
     231           sage: f = ECM() 
     232           sage: n = 508021860739623467191080372196682785441177798407961 
     233           sage: f.find_factor(n) 
     234           [79792266297612017, 6366805760909027985741435139224233] 
    156235        """ 
    157236        if not 'c' in kwds: kwds['c'] = 1000000000 
Note: See TracChangeset for help on using the changeset viewer.