Package: primefac
primefac version 2.0.12This is a module and command-line utility for factoring integers.  As a module, we provide a primality test, several functions for extracting a non-trivial factor of an integer, a generator that yields all of a number’s prime factors (with multiplicity), and ancillary functions used in calculating these things.  As a command-line utility, this project aims to replace GNU’sfactorcommand with a more versatile utility — in particular, this utility can operate on arbitrarily large numbers, uses multiple cores in parallel, uses better algorithms, handles input in reverse Polish notation, and can be tweaked via command-line flags.  Specifically,GNU’sfactorcommand won’t factor anything greater than 2127-1.  primefac handles arbitrarily large integers.GNU’sfactorcommand uses Pollard’s rho algorithm.  While this extracts small factors quickly, large factors take a while to find.  primefac uses, among other things, the elliptic curve method and the self-initializing quadratic sieve, which are far more efficient at extracting large factors.GNU’sfactorcommand is a single-threaded application.  primefac uses by default five threads to take advantage of the multiple cores typically available on modern machines.  Each of these threads uses a different algorithm to factor the number:One thread runs Brent’s variation on Pollard’s rho algorithm.  This is good for extracting smallish factors quickly.One thread runs the two-stage version of Pollard’sp-1 method.  This is good at finding factorspfor whichp-1 is a product of small primes.One thread runs Williams’p+1 method.  This is good at finding factorspfor whichp+1 is a product of small primes.One thread runs the elliptic curve method.  This is a bit slower than Pollard’s rho algorithm when the factors extracted are small, but it has significantly better performance on difficult factors.One thread runs the self-initializing quadratic sieve.  This is the best algorithm for factoring “hard” numbers short of the general number field sieve.  However, it’s (relatively speaking) more than a little slow when the numbers are small, and the time it takes depends only on the size of the number being factored rather than the size of the factors being extracted as with Pollard’s rho algorithm and the elliptic curve method, so we use the preceding algorithms to handle those.We also extend the utility by interpreting the command-line arguments as an expression in reverse Polish notation and factoring the numbers remaining on the evaluation stack when interpretation is complete.  For example, the command:python3 -m primefac 24 ! 1 - 38 ! 1 +will factor the numbers 24! - 1 = 620448401733239439359999 and 38! + 1 = 523022617466601111760007224100074291200000001.Module UsageThe primary functions areisprimeandprimefac, but we define a number of helper functions along the way.  A few of these functions are already available via Python3’s built-in math module, but get defined anyway for the sake of PyPy3 compatibility.isqrt(n)Computes the greatest integer whose square does not exceed the non-negative integern.introot(n,r=2)For non-negativen, returns the greatest integer less than or equal to therthroot ofn.For negativen, returns the least integer greater than or equal to therthroot ofn, orNoneifris even.primegen(limit=inf)Non-terminating generator.  Yields the prime numbers.  It amounts to a segmented Sieve of Eratosthenes.iterprod(l)Returns the product of the elements ofl, which can be any iterable (but should obviously terminate; e.g.,iterprod(primegen())would be a bad idea).jacobi(a,p)Computes the Jacobi symbol(a|p), wherepis a positive odd number.  This is used inisprime.isprime(n,tb=(3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59))The main primality test.  It iss an implementation of the BPSW test (Baillie-Pomerance-Selfridge-Wagstaff) and is deterministic for all numbers less than 264— in fact, while infinitely many false positives are conjectured to exist, no false positives are currently known.  We preface the test with some trial division for the sake of speed.  We trial divide by 2 whether the caller wants to or not.ilog(x,b)Returns the greatest integerlsuch thatb**l<= x.ispower(n,r=0)Ifr == 0, then we return the largest integer that, when squared/cubed/etc, yieldsn, orNoneif no such integer exists.  Ifr != 0, then we check whethernis a perfectr``thpower, returning its``r``throot if it is and ``Noneif it is not.pollardrho_brent(n)Brent’s improvement on Pollard’s rho algorithm.  Returnsnifnis prime; otherwise, we keep chugging until we find a factor ofnstrictly between1andn.pollard_pm1(n,B1=100,B2=1000)Pollard’sp+1 algorithm, two-phase version.  Returnsnifnis prime; otherwise, we keep chugging until we find a factor ofnstrictly between1andn.mlucas(v,a,n)Helper function forwilliams_pp1.  Multiplies along a Lucas sequence modulon.williams_pp1(n)Williams’p+1 algorithm.  Returnsnifnis prime; otherwise, we keep chugging until we find a factor ofnstrictly between1andn.ecadd(p1,p2,p0,n)Helper function forecm.  Adds points on a Montgomery curve modulon.ecdub(p,A,n)Helper function forecm.  Doubles a point on a Montgomery curve modulon.ecmul(m,p,A,n)Helper function forecm.  Multiplies a point on a Montgomery curve modulon.secm(n,B1,B2,seed)Seeded ECM.  Helper function forecm.  Returns a possibly-trivial divisor of n given two bounds and a seed.  Uses the two-phase algorithm on Montgomery curves.  Seehttps://wp.me/prTJ7-zIandhttps://wp.me/prTJ7-A7for more details.  Most of the code for this function’s “helpers” were shamelessly copied from the first of those links.ecmparams(n)Generates a sequence of parameters to be applied tosecm.ecm(n,paramseq=ecmparams,nprocs=1)“Modern” integer factoring via elliptic curves.  Uses Montgomery curves, the two-phase algorithm, and (optionally) multiple processes.  The hard work is done by secm(); this function just does the managerial work of pulling a sequence of parameters out of a generator and feeding them into secm().  Returnsnifnis prime; otherwise, we keep chugging until we find a factor ofnstrictly between1andn.  For more details seethesetwoProgramming Praxis posts.sqrtmod_prime(n,p)Computes a square root ofnmodulo the prime numberp.  The return value is not meaningful ifnhas no square root modulopor ifpis composite.modinv(a,m)Computes a multiplicative inverse ofamodulom.  The return value is not meaningful ifgcd(a,m) != 1.siqs(n)Factorsnusing the self-initializing quadratic sieve.  Returnsnifnis prime; otherwise, we keep chugging until we find a factor ofnstrictly between1andn.  This function was copied mostly verbatim fromthis GitHub repository.multifactor(n,methods=(pollardrho_brent,pollard_pm1,williams_pp1,ecm,siqs))Runs several factoring algorithms onnsimultaneously by loading them into their own threads via themultiprocessingmodule.  When one function returns, everything is killed off and that value gets returned.primefac(n,trial_limit=1000,rho_rounds=42000,verbose=False,methods=(pollardrho_brent))Generator.  Yields the prime factors ofn, with multiplicity.rpn(instr)Evaluates the stringinstras an arithmetical expression in reverse Polish notation.
The available binary operators are +, -, *, //, %, and **, which all indicate the same operations here as they indicate in Python3 source code; i.e., they denote addition, subtraction, multiplication, floor division, remaindering, and powering.  The available unary operators are ! and #, which denote the factorial and primorial, respectively.  For terminal syntax compatibility reasons, the RPN expression may be enclosed in quotes, and five aliases are allowed: x for *, / for //, xx for **, f for !, and p for #.DependenciesThis package imports items frommultiprocessing,random,itertools,math,time, anddatetime.  These are all in the Python standard library.Command-Line Usagepython3-mprimefac[-vs|-sv][-v|--verbose][-s|--summary|--summarize][-t=NUMBER][-r=NUMBER][-m=[prb][,p-1][,p+1][,ecm][,siqs]]rpnrpnis an expression in revese Polish notation and is evaluated using integer arithmetic.  Each number that remains on the stack after evaluation is then factored.-tsets the trial division limit; the default value is 1000.  Use-t=infto use trial division exclusively.-rsets the number of rounds of Pollard’s rho algorithm to try before calling a factor “difficult”.  The default value is 42,000.  Use-r=infto use Pollard’s rho exclusively once the trial division is completed.If verbosity is invoked, then we provide progress reports and also state which algorithms produced which factors during the multifactor phase.If the summary and verbosity flags are absent, then the output should be identical to the output of the GNU factor command, modulo permutation of the factors.  If the verbosity flag is invoked, then we provide progress reports, turn on the summary flag, and state which methods yielded which factors during the multifactor phase.If the summary flag is present, then the output is modified by including a single newline between each item’s output, before the first, and after the last.  Each item’s output is also modified by printing a second line of data summarizing the results by indicating the number of digits (base 10) in the input, the number of digits (base 10) in each factor, and the factors’ multiplicities.  For example:>>> user@computer:~$ python3 -m primefac  -s   24 ! 1 -   7 f
>>>
>>> 620448401733239439359999: 991459181683 625793187653
>>> Z24  =  P12 x P12  =  625793187653 x 991459181683
>>>
>>> 5040: 2 2 2 2 3 3 5 7
>>> Z4  =  P1^4 x P1^2 x P1 x P1  =  2^4 x 3^2 x 5 x 7
>>>
>>> user@computer:~$Note that primes in the ordinary output lines are listed in the order in which they were found, while primes in the summary lines are reported in strictly-increasing order.The-vand-sflags may be combined into a single flag in either order — i.e., into-vsor-sv.The-m=flag controls the functions used during themultifactorphase.  The options areprb,p-1,p+1,ecm, andsiqs, representing Pollard’s rho, Pollard’sp-1, Williams’p+1, the elliptic curve method, and the self-initializing quadratic sieve, respectively.  The options must be separated by commas.  The options can be repeated: ifprbis listed twice, for example, thenmultifactorwill run two instances ofpollardrho_brentsimultaneously.  In the case ofprbandecm, this decreases the expectation value of the time to find a factor, whereas the other three algorithms (p-1,p+1, and MPQS) have no randomized component so that running duplicate instances of these three algorithms confers no benefit.  We therefore ignore repeated listings of the latter three methods: for example, callingpython3-mprimefac-m=prb,prb,ecm,ecm,ecm,mpqs,mpqs38!1+will run during the multifactor phase two instances of Pollard’s rho, three instances of the elliptic curve method, and one instance of the MQPS.  Invoking more methods than you have cores available is unlikely to confer any benefit.