Navigation

Source code for sympy.ntheory.primetest

"""Primality testing"""from__future__importprint_function,divisionfromsympy.core.compatibilityimportxrange# pseudoprimes that will pass through last mr_safe test_pseudos=set([669094855201,1052516956501,2007193456621,2744715551581,9542968210729,17699592963781,19671510288601,24983920772821,24984938689453,29661584268781,37473222618541,46856248255981,47922612926653,48103703944453,49110566041153,49752242681221,91206655032481,91481980096033,119034193492321,123645258399601,128928036060253,137364148720147,150753857310253,153131886327421,155216912613121,185610214763821,224334357392701,227752294950181,230058334559041,304562854940401,306001576998253,335788261073821,377133492079081,379242177424951,389970770948461,397319638319521,448114903362253,523235160050221,628999496281621,699349238838253,746667678235753,790198268451301,794036495175661,823820871230281,867739535711821,1039918661294761,1099127938585141,1104388025338153,1173374598605653,1262797719066157,1265872947674653,1325898212229667,1327034517143653,1418575746675583,1666122072463621,1837400535259453,1857422490084961,1870756820971741,1914550540480717,2018963273468221,2163829000939453,2206020317369221,2301037384029121,2416062055125421,2435076500074921,2545656135020833,2594428516569781,2669983768115821,2690937050990653,2758640869506607,2833525461416653,2876662942007221,2932155806957821,2957010595723801,3183606449929153,3220133449185901,3424103775720253,3625360152399541,3939300299037421,3947917710714841,3980273496750253,4182256679324041,4450605887818261,4727893739521501,4750350311306953,4755334362931153,5756440863559753,5760976603475341,5794399356078761,5954850603819253,6125544931991761,6320931714094861,6347593619672581,6406268028524101,6510632945054941,6620082224794741,6627325072566061,6844056606431101,6989404981060153,7144293947609521,7288348593229021,7288539837129253,7406102904971689,7430233301822341,7576425305871193,7601696719033861,7803926845356487,7892007967006633,7947797946559453,8207000460596953,8295064717807513,8337196000698841,8352714234009421,8389755717406381,8509654470665701,8757647355282841,8903933671696381,8996133652295653,9074421465661261,9157536631454221,9188353522314541])def_test(n,base,s,t):"""Miller-Rabin strong pseudoprime test for one base. Return False if n is definitely composite, True if n is probably prime, with a probability greater than 3/4. """# do the Fermat testb=pow(base,t,n)ifb==1orb==n-1:returnTrueelse:forjinxrange(1,s):b=pow(b,2,n)ifb==n-1:returnTrue# see I. Niven et al. "An Introduction to Theory of Numbers", page 78ifb==1:returnFalsereturnFalse

[docs]defisprime(n):""" Test if n is a prime number (True) or not (False). For n < 10**16 the answer is accurate; greater n values have a small probability of actually being pseudoprimes. Negative primes (e.g. -2) are not considered prime. The function first looks for trivial factors, and if none is found, performs a safe Miller-Rabin strong pseudoprime test with bases that are known to prove a number prime. Finally, a general Miller-Rabin test is done with the first k bases which will report a pseudoprime as a prime with an error of about 4**-k. The current value of k is 46 so the error is about 2 x 10**-28. Examples ======== >>> from sympy.ntheory import isprime >>> isprime(13) True >>> isprime(15) False See Also ======== sympy.ntheory.generate.primerange : Generates all primes in a given range sympy.ntheory.generate.primepi : Return the number of primes less than or equal to n sympy.ntheory.generate.prime : Return the nth prime """n=int(n)ifn<2:returnFalseifn&1==0:returnn==2ifn<=23001:returnpow(2,n,n)==2andnnotin[341,561,645,1105,1387,1729,1905,2047,2465,2701,2821,3277,4033,4369,4371,4681,5461,6601,7957,8321,8481,8911,10261,10585,11305,12801,13741,13747,13981,14491,15709,15841,16705,18705,18721,19951,23001]try:return_mr_safe(n)exceptValueError:# prime list to use when number must be tested as a probable prime;# these are the 46 primes less than 200bases=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199]returnmr(n,bases)

def_mr_safe_helper(_s):""" Analyze a (new) mr_safe line for for total number of s's to be tested in _test along with the primes that must be cleared by a previous test. e.g. >>> from sympy.ntheory.primetest import _mr_safe_helper >>> print(_mr_safe_helper("if n < 170584961: return mr(n, [350, 3958281543])")) # [350, 3958281543] stot = 1 clear [2, 3, 5, 7, 29, 67, 679067] >>> print(_mr_safe_helper('return mr(n, [2, 379215, 457083754])')) # [2, 379215, 457083754] stot = 1 clear [2, 3, 5, 53, 228541877] """def_info(bases):""" Analyze the list of bases, reporting the number of 'j-loops' that will be required if this list is passed to _test (stot) and the primes that must be cleared by a previous test. This info tag should then be appended to any new mr_safe line that is added so someone can easily see whether that line satisfies the requirements of mr_safe (see docstring there for details). """fromsympy.ntheory.factor_importfactorint,trailingfactors=[]tot=0forbinbases:tot+=trailing(b-1)f=factorint(b)factors.extend(f)factors=sorted(set(factors))bases=sorted(set(bases))ifbases==factors:factors='== bases'else:factors=str(factors)return' # %s stot = %s clear %s'%tuple([str(x).replace('L','')forxin(list(bases),tot,factors)])_r=[int(_x)for_xin_s.split('[')[1].split(']')[0].split(',')]return_info(_r)