K iOcdZddlmZddlmZddlmZmZm Z m Z m Z m Z mZmZmZmZddlmZddlmZmZdZdZd Zd Zdd Z d Zd ZdZdZdZdZ dZ!dZ"dZ#gdZ$dZ%dZ&y)z Primality testing )count)sympify) gmpygcdjacobi is_square bit_scan1 is_fermat_prp is_euler_prpis_selfridge_prpis_strong_selfridge_prpis_strong_bpsw_prp)_lucas_sequence)as_int filldedent)4 =Yki i_iiii iiGi%i&i+iMiTiZiѭiPiiiLig i) i;1iUii-iA.idji%}i_@ini1ii,i;6iYiؑiAsi1Pli{i8i!wczt|t|}}|dk(r|dk(xst|dzSt||S)aReturns True if ``n`` is prime or is an odd composite integer that is coprime to ``a`` and satisfy the modular arithmetic congruence relation: .. math :: a^{n-1} \equiv 1 \pmod{n} (where mod refers to the modulo operation). Parameters ========== n : Integer ``n`` is a positive integer. a : Integer ``a`` is a positive integer. ``a`` and ``n`` should be relatively prime. Returns ======= bool : If ``n`` is prime, it always returns ``True``. The composite number that returns ``True`` is called an Fermat pseudoprime. Examples ======== >>> from sympy.ntheory.primetest import is_fermat_pseudoprime >>> from sympy.ntheory.factor_ import isprime >>> for n in range(1, 1000): ... if is_fermat_pseudoprime(n, 2) and not isprime(n): ... print(n) 341 561 645 References ========== .. [1] https://en.wikipedia.org/wiki/Fermat_pseudoprime r)rboolr nas ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/ntheory/primetest.pyis_fermat_pseudoprimer%sAR !9fQiqAAvAv$a!e$ A c0t|t|}}|dkr td|dkr td|dk(ry|dk(r|dk(xst|dzS|dzdk(r|dk(St||dk7r tdt ||dz dz|d|dz fvS)aReturns True if ``n`` is prime or is an odd composite integer that is coprime to ``a`` and satisfy the modular arithmetic congruence relation: .. math :: a^{(n-1)/2} \equiv \pm 1 \pmod{n} (where mod refers to the modulo operation). Parameters ========== n : Integer ``n`` is a positive integer. a : Integer ``a`` is a positive integer. ``a`` and ``n`` should be relatively prime. Returns ======= bool : If ``n`` is prime, it always returns ``True``. The composite number that returns ``True`` is called an Euler pseudoprime. Examples ======== >>> from sympy.ntheory.primetest import is_euler_pseudoprime >>> from sympy.ntheory.factor_ import isprime >>> for n in range(1, 1000): ... if is_euler_pseudoprime(n, 2) and not isprime(n): ... print(n) 341 561 References ========== .. [1] https://en.wikipedia.org/wiki/Euler_pseudoprime rz%a should be an integer greater than 0z%n should be an integer greater than 0Frrz*The two numbers should be relatively prime)r ValueErrorr rpowr!s r$is_euler_pseudoprimer*IsP !9fQiqA1u@AA1u@AAAvAvAv$a!e$1uzAv  1ayA~EFF q1q5Q, "q!a%j 00r&czt|t|}}|dk(r|dk(xst|dzSt||S)a Returns True if ``n`` is prime or is an odd composite integer that is coprime to ``a`` and satisfy the modular arithmetic congruence relation: .. math :: a^{(n-1)/2} \equiv \left(\frac{a}{n}\right) \pmod{n} (where mod refers to the modulo operation). Parameters ========== n : Integer ``n`` is a positive integer. a : Integer ``a`` is a positive integer. ``a`` and ``n`` should be relatively prime. Returns ======= bool : If ``n`` is prime, it always returns ``True``. The composite number that returns ``True`` is called an Euler-Jacobi pseudoprime. Examples ======== >>> from sympy.ntheory.primetest import is_euler_jacobi_pseudoprime >>> from sympy.ntheory.factor_ import isprime >>> for n in range(1, 1000): ... if is_euler_jacobi_pseudoprime(n, 2) and not isprime(n): ... print(n) 561 References ========== .. [1] https://en.wikipedia.org/wiki/Euler%E2%80%93Jacobi_pseudoprime rr)rr r r!s r$is_euler_jacobi_pseudoprimer,sAN !9fQiqAAvAv$a!e$ 1 r&cH|rt|}|dkry|dvryt|S)aReturn True if n == a * a for some integer a, else False. If n is suspected of *not* being a square then this is a quick method of confirming that it is not. Examples ======== >>> from sympy.ntheory.primetest import is_square >>> is_square(25) True >>> is_square(2) False References ========== .. [1] https://mersenneforum.org/showpost.php?p=110896 See Also ======== sympy.core.intfunc.isqrt rF)rrT)rgmpy_is_square)r"preps r$rrs/. 1I q5 ; ! r&ct|||}|dk(s||dz k(ryt|dz D] }t|d|}||dz k(ry|dk(s yy)zMiller-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. rTrF)r)range)r"basestb_s r$_testr7sf D!QAAva!e 1q5\ 1aL A: 6  r&cddlm}t|}|dks |dkDr |dzdk(ryt|dz }||z }|D]*}||k\r||z}|dk\s||}t ||||r*yy)aGPerform a Miller-Rabin strong pseudoprime test on n using a given list of bases/witnesses. References ========== .. [1] Richard Crandall & Carl Pomerance (2005), "Prime Numbers: A Computational Perspective", Springer, 2nd edition, 135-138 A list of thresholds and the bases they require are here: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Deterministic_variants Examples ======== >>> from sympy.ntheory.primetest import mr >>> mr(1373651, [2, 3]) False >>> mr(479001599, [31, 73]) True r)ZZrFrT)sympy.polys.domainsr9rr r7)r"basesr9r3r4r2s r$mrr<s.'q A1uQ1q5A:!a%A QA 19 AID 19d8DD!Q' r&c~tdD]/}|dzdz }t||}|dk(r||dfcS|dk(s)||zs/yy) aCalculates the "extra strong" parameters (D, P, Q) for n. Parameters ========== n : int positive odd integer Returns ======= D, P, Q: "extra strong" parameters. ``(0, 0, 0)`` if we find a nontrivial divisor of ``n``. Examples ======== >>> from sympy.ntheory.primetest import _lucas_extrastrong_params >>> _lucas_extrastrong_params(101) (12, 4, 1) >>> _lucas_extrastrong_params(15) (0, 0, 0) References ========== .. [1] OEIS A217719: Extra Strong Lucas Pseudoprimes https://oeis.org/A217719 .. [2] https://en.wikipedia.org/wiki/Lucas_pseudoprime rrrr)rrrN)rr)r"PDjs r$_lucas_extrastrong_paramsrC sR>1X qD1H 1aL 7q!9  !VA r&c:t|}|dkryt|S)aStandard Lucas compositeness test with Selfridge parameters. Returns False if n is definitely composite, and True if n is a Lucas probable prime. This is typically used in combination with the Miller-Rabin test. References ========== .. [1] Robert Baillie, Samuel S. Wagstaff, Lucas Pseudoprimes, Math. Comp. Vol 35, Number 152 (1980), pp. 1391-1417, https://doi.org/10.1090%2FS0025-5718-1980-0583518-6 http://mpqs.free.fr/LucasPseudoprimes.pdf .. [2] OEIS A217120: Lucas Pseudoprimes https://oeis.org/A217120 .. [3] https://en.wikipedia.org/wiki/Lucas_pseudoprime Examples ======== >>> from sympy.ntheory.primetest import isprime, is_lucas_prp >>> for i in range(10000): ... if is_lucas_prp(i) and not isprime(i): ... print(i) 323 377 1159 1829 3827 5459 5777 9071 9179 rF)rr r"s r$ is_lucas_prprF4s#D q A1u A r&c:t|}|dkryt|S)a-Strong Lucas compositeness test with Selfridge parameters. Returns False if n is definitely composite, and True if n is a strong Lucas probable prime. This is often used in combination with the Miller-Rabin test, and in particular, when combined with M-R base 2 creates the strong BPSW test. References ========== .. [1] Robert Baillie, Samuel S. Wagstaff, Lucas Pseudoprimes, Math. Comp. Vol 35, Number 152 (1980), pp. 1391-1417, https://doi.org/10.1090%2FS0025-5718-1980-0583518-6 http://mpqs.free.fr/LucasPseudoprimes.pdf .. [2] OEIS A217255: Strong Lucas Pseudoprimes https://oeis.org/A217255 .. [3] https://en.wikipedia.org/wiki/Lucas_pseudoprime .. [4] https://en.wikipedia.org/wiki/Baillie-PSW_primality_test Examples ======== >>> from sympy.ntheory.primetest import isprime, is_strong_lucas_prp >>> for i in range(20000): ... if is_strong_lucas_prp(i) and not isprime(i): ... print(i) 5459 5777 10877 16109 18971 rF)rr rEs r$is_strong_lucas_prprH\s#@ q A1u "1 %%r&c@t|}|dk(ry|dks|dzdk(ryt|ryt|\}}}|dk(ryt|dz}|dz|z }t ||||\}}}|dk(r|dk(s||dz k(ryt d|D]}|dk(ry||zdz |z}y)aCExtra Strong Lucas compositeness test. Returns False if n is definitely composite, and True if n is an "extra strong" Lucas probable prime. The parameters are selected using P = 3, Q = 1, then incrementing P until (D|n) == -1. The test itself is as defined in [1]_, from the Mo and Jones preprint. The parameter selection and test are the same as used in OEIS A217719, Perl's Math::Prime::Util, and the Lucas pseudoprime page on Wikipedia. It is 20-50% faster than the strong test. Because of the different parameters selected, there is no relationship between the strong Lucas pseudoprimes and extra strong Lucas pseudoprimes. In particular, one is not a subset of the other. References ========== .. [1] Jon Grantham, Frobenius Pseudoprimes, Math. Comp. Vol 70, Number 234 (2001), pp. 873-891, https://doi.org/10.1090%2FS0025-5718-00-01197-2 .. [2] OEIS A217719: Extra Strong Lucas Pseudoprimes https://oeis.org/A217719 .. [3] https://en.wikipedia.org/wiki/Lucas_pseudoprime Examples ======== >>> from sympy.ntheory.primetest import isprime, is_extra_strong_lucas_prp >>> for i in range(20000): ... if is_extra_strong_lucas_prp(i) and not isprime(i): ... print(i) 989 3239 5777 10877 rTrFr)rr.rCr rr1) r"rAr@Qr3kUVr6s r$is_extra_strong_lucas_prprNsZ q AAv1uQ1 a'*GAq!Av !a%A Q1 AaAq)GAq!Av16Q!a%Z 1a[ 6 qS1WM r&ct|}|dkr tdt|dz }||z }||jkr td|dzdk(r|dk(S|dzrt d|dz ||dz k(St |ryt d|D]2}t||}|dk(rt ||dz ||dz k(cS|dk(s2yy) a< Test if the Proth number `n = k2^m + 1` is prime. where k is a positive odd number and `2^m > k`. Parameters ========== n : Integer ``n`` is Proth number Returns ======= bool : If ``True``, then ``n`` is the Proth prime Raises ====== ValueError If ``n`` is not Proth number. Examples ======== >>> from sympy.ntheory.primetest import proth_test >>> proth_test(41) True >>> proth_test(57) False References ========== .. [1] https://en.wikipedia.org/wiki/Proth_prime rzn is not Proth numberrrFrr?N)rr(r bit_lengthr)r.r1r)r"mrKr#rBs r$ proth_testrRsF q A1u011!a%A QA1<<>0111uzAv 1u1a1fa AE))a1a[ 1aL 7q!q&!$A- - 6 r&cfd}d|zdz }t|dz D]}t|d|dz }|dk(S)a Test if the Mersenne number `M_p = 2^p-1` is prime. Parameters ========== p : int ``p`` is an odd prime number Returns ======= bool : If ``True``, then `M_p` is the Mersenne prime Examples ======== >>> from sympy.ntheory.primetest import _lucas_lehmer_primality_test >>> _lucas_lehmer_primality_test(5) # 2**5 - 1 = 31 is prime True >>> _lucas_lehmer_primality_test(11) # 2**11 - 1 = 2047 is not prime False See Also ======== is_mersenne_prime References ========== .. [1] https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test r>rrr)r1r))pvrQr6s r$_lucas_lehmer_primality_testrVsID A 1qA 1q5\ 1aL1  6Mr&ct|}|dkry||dzzry|j}|tvry|dks t|syt |}|rt t d|z|S)aReturns True if ``n`` is a Mersenne prime, else False. A Mersenne prime is a prime number having the form `2^i - 1`. Examples ======== >>> from sympy.ntheory.factor_ import is_mersenne_prime >>> is_mersenne_prime(6) False >>> is_mersenne_prime(127) True References ========== .. [1] https://mathworld.wolfram.com/MersennePrime.html rFTi@z` This Mersenne Prime, 2^%s - 1, should be added to SymPy's known values.)rrPMERSENNE_PRIME_EXPONENTSisprimerVr(r)r"rTresults r$is_mersenne_primer[.s( q A1uAE{ A $$:~WQZ )! ,F %134%567 7 Mr&(i<iiii>i)i/iii i$i0i8 i\ia iii,iix$ii^ iOi%(i\iiKiliiiiq i5iSii2iW iii#ii#iiiDiyi'ii i,iii r^i i iii- i)i ri;ii:iiZiii i6iiiiii_ii`i iiH ii6i)iMi& i3i"i;iiii`i#ii ii i i i[)i.'iI inii_iiiiii1iixiYi*iii:r_i}i i i8 ii" iUiiRi!i i iiiiviTi-i\iJiFi'ii i& i8iCrbi_i"'iH1iX&i[iiKip*iYr]iiiiDii7i;iPiQii i iXi#iiNi ii"iaiiJipi6i*ii ii iiiA'ifii i iibiiiiiiizi ihi i`iIiYi *ii|r\iisi i0iivIiii iMi4ii i i+ii^iZidiy ig idiiUii XiQ,iict|}|dvry|dks|dzdk(s|dzdk(s|dzdk(ry|dkry|d zdk(sX|d zdk(sP|d zdk(sH|d zdk(s@|d zdk(s8|dzdk(s0|dzdk(s(|dzdk(s |dzdk(s|dzdk(s|dzdk(s|dzdk(ry|dkry|dkrtd|dz |d|dz fvxr|dvSddlm}||jdkr|j |\}}||k(Sddlm}|j|x}||k(St t|S|dkr t|dgS|dkr5|dz |z d z}|dz |z d z}|dz |z d!z}t|t|gS|d"krt|gd#S|d$krt|gd%S|d&krt|gd'S|d(krt|gd)S|d*krt|gd+S|d,krt|gd-S|d.krt|gd/St|S)0a Test if n is a prime number (True) or not (False). For n < 2^64 the answer is definitive; larger n values have a small probability of actually being pseudoprimes. Negative numbers (e.g. -2) are not considered prime. The first step is looking for trivial factors, which if found enables a quick return. Next, if the sieve is large enough, use bisection search on the sieve. For small numbers, a set of deterministic Miller-Rabin tests are performed with bases that are known to have no counterexamples in their range. Finally if the number is larger than 2^64, a strong BPSW test is performed. While this is a probable prime test and we believe counterexamples exist, there are no known counterexamples. Examples ======== >>> from sympy.ntheory import isprime >>> isprime(13) True >>> isprime(15) False Notes ===== This routine is intended only for integer input, not numerical expressions which may represent numbers. Floats are also rejected as input because they represent numbers of limited precision. While it is tempting to permit 7.0 to represent an integer there are errors that may "pass silently" if this is allowed: >>> from sympy import Float, S >>> int(1e3) == 1e3 == 10**3 True >>> int(1e23) == 1e23 True >>> int(1e23) == 10**23 False >>> near_int = 1 + S(1)/10**19 >>> near_int == int(near_int) False >>> n = Float(near_int, 10) # truncated by precision >>> n % 1 == 0 True >>> n = Float(near_int, 20) >>> n % 1 == 0 False See Also ======== sympy.ntheory.generate.primerange : Generates all primes in a given range sympy.functions.combinatorial.numbers.primepi : Return the number of primes less than or equal to n sympy.ntheory.generate.prime : Return the nth prime References ========== .. [1] https://en.wikipedia.org/wiki/Strong_pseudoprime .. [2] Robert Baillie, Samuel S. Wagstaff, Lucas Pseudoprimes, Math. Comp. Vol 35, Number 152 (1980), pp. 1391-1417, https://doi.org/10.1090%2FS0025-5718-1980-0583518-6 http://mpqs.free.fr/LucasPseudoprimes.pdf .. [3] https://en.wikipedia.org/wiki/Baillie-PSW_primality_test )rrrTrrrrF1r rrrr%ra+/i i5r)i i{i/ii=)siever?) factor_cachei6lj_| l)W1i;]l?_F)l|a?bUl$|3r_ l(P7lk^AH)rl@0le7:`V!l"SV=Q-O lv-)rlmorl\^E~8l)VmAlvKHu8l~_!@)rl\]AlbY+5l0PrlMK Tl0KOM l)riEi$inii=ikl7 y_@I7) rrrrrlrrrrmrnrrol%!Hn fW) rrrrrlrrrrmrnrrora) rr)sympy.ntheory.generaterr_listsearchsympy.ntheory.factor_rsget_gmpyrr< _MR_BASES_32)r"r3lursreths r$rYrYssJ q A  I~1uQ1 Q1 Q1 2v Q1}RA !b&Q1r6a- B1}RA !b&Q1r6a- B1}RA !b&Q1r6a-4x5y1a1fa QAJ.^1<^3^^2AGGBKxx{1Av 2""/ax  !!$$ 6z!)*++:~2g]i '2g]i '2g]c !!l1o&''<!VWW>!TUU !dee !wxx !GHH ##!ABB $$!EFFJ a  r&c.t|}|j\}}t|d}t|d}|dk(r t|}t |xr|dzdk(S|dk(r t|}t |xr|dzdk(St |dz|dzzS)z}Test if num is a Gaussian prime number. References ========== .. [1] https://oeis.org/wiki/Gaussian_primes F)strictrr>rr)r as_real_imagrabsrY)numr#r5s r$is_gaussian_primer+s #,C    DAqqAqAAv Fqz(a!eqj( a Fqz(a!eqj( 1a4!Q$; r&N)T)'__doc__ itertoolsrsympy.core.sympifyrsympy.external.gmpyrr{rrrr.r r r r r rsympy.external.ntheoryrsympy.utilities.miscrrrXr%r*r,r7r<rCrFrHrNrRrVr[r|rYrr&r$rs &555 33 ,^51p*Z@('T%P%P#&LEP8v&R'TN 6n!p r&