
    \hOc                         S r SSKJr  SSKJr  SSKJrJrJ	r	J
rJrJrJrJrJrJr  SSKJr  SSKJrJr  SrS rS	 rS
 rSS jr
S rS rS rS rS rS r S r!S r"S r#/ SQr$S r%S r&g)z
Primality testing

    )count)sympify)
gmpygcdjacobi	is_square	bit_scan1is_fermat_prpis_euler_prpis_selfridge_prpis_strong_selfridge_prpis_strong_bpsw_prp)_lucas_sequence)as_int
filldedent)4                        =   Y   k      i	  i_  i  i  i  i  i  iG  i%  i&  i+  iM  iT  iZ  iѭ  iP i i iL ig i) i;1 iU ii- iA. idj i%} i_@ini1ii,i;6iYiؑiAsi1Pli{i8i!wc                     [        U 5      [        U5      pUS:X  a  U S:H  =(       d    [        U S-  5      $ [        X5      $ )al  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} \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   )r   boolr
   nas     O/var/www/auris/envauris/lib/python3.13/site-packages/sympy/ntheory/primetest.pyis_fermat_pseudoprimer%      s=    R !9fQiqAvAv$a!e$    c                 B   [        U 5      [        U5      pUS:  a  [        S5      eU S:  a  [        S5      eU S:X  a  gUS:X  a  U S:H  =(       d    [        U S-  5      $ U S-  S:X  a  U S:H  $ [        X5      S:w  a  [        S5      e[	        XS-
  S-  U 5      SU S-
  4;   $ )al  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 \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
r   z%a should be an integer greater than 0z%n should be an integer greater than 0Fr   r   z*The two numbers should be relatively prime)r   
ValueErrorr    r   powr!   s     r$   is_euler_pseudoprimer*   I   s    P !9fQiq1u@AA1u@AAAvAvAv$a!e$1uzAv
1yA~EFFqq5Q,"q!a%j00r&   c                     [        U 5      [        U5      pUS:X  a  U S:H  =(       d    [        U S-  5      $ [        X5      $ )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
r   r   )r   r    r   r!   s     r$   is_euler_jacobi_pseudoprimer,      s=    N !9fQiqAvAv$a!e$r&   c                 X    U(       a  [        U 5      n U S:  a  gU S;   a  g[        U 5      $ )a  Return 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
r   F)r   r   T)r   gmpy_is_square)r"   preps     r$   r   r      s/    . 1Iq5;!r&   c                     [        XU 5      nUS:X  d  X@S-
  :X  a  g[        US-
  5       H"  n[        USU 5      nX@S-
  :X  a    gUS:X  d  M"    g   g)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.

r   Tr   F)r)   range)r"   basestb_s         r$   _testr7      s[     	DQAAv!e1q5\1aLA:6  r&   c                     SSK Jn  [        U 5      n U S:  d  U S:  a
  U S-  S:X  a  g[        U S-
  5      nX-	  nU H/  nXP:  a  XP-  nUS:  d  M  U" U5      n[	        XX45      (       a  M/    g   g)a  Perform 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   )ZZr   Fr   T)sympy.polys.domainsr9   r   r	   r7   )r"   basesr9   r3   r4   r2   s         r$   mrr<      sz    . 'q	A1uQ1q5A:!a%A	A9ID19d8D!''  r&   c                     [        S5       H5  nUS-  S-
  n[        X 5      nUS:X  a  X!S4s  $ US:X  d  M*  X -  (       d  M5    g   g)	a>  Calculates 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

r   r      r   r   )r   r   r   N)r   r   )r"   PDjs       r$   _lucas_extrastrong_paramsrC     sI    > 1XqD1H1L7!9!V r&   c                 <    [        U 5      n U S:  a  g[        U 5      $ )a0  Standard 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
r   F)r   r   r"   s    r$   is_lucas_prprF   4  s#    D 	q	A1uAr&   c                 <    [        U 5      n U S:  a  g[        U 5      $ )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
r   F)r   r   rE   s    r$   is_strong_lucas_prprH   \  s#    @ 	q	A1u"1%%r&   c                 R   [        U 5      n U S:X  a  gU S:  d	  U S-  S:X  a  g[        U 5      (       a  g[        U 5      u  pnUS:X  a  g[        U S-   5      nU S-   U-	  n[	        XX55      u  pgnUS:X  a  US:X  d  XpS-
  :X  a  g[        SU5       H  nUS:X  a    gXw-  S-
  U -  nM     g)a  Extra 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
r   Tr   Fr   )r   r.   rC   r	   r   r1   )	r"   rA   r@   Qr3   kUVr6   s	            r$   is_extra_strong_lucas_prprN     s    Z 	q	AAv1uQ1a'*GA!Av 	!a%A	
Q1AaA)GA!Av16Qa%Z1a[6S1WM  r&   c                    [        U 5      n U S:  a  [        S5      e[        U S-
  5      nX-	  nXR                  5       :  a  [        S5      eU S-  S:X  a  U S:H  $ US-  (       a  [	        SU S-	  U 5      U S-
  :H  $ [        U 5      (       a  g[        SU 5       H3  n[        X05      nUS:X  a  [	        X0S-	  U 5      U S-
  :H  s  $ US:X  d  M3    g   g)	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

r   zn is not Proth numberr   r   Fr   r?   N)r   r(   r	   
bit_lengthr)   r.   r1   r   )r"   mrK   r#   rB   s        r$   
proth_testrR     s    F 	q	A1u011!a%A	A<<>0111uzAv1u1a1fa AE))a 1a[1L7qq&!$A--6 r&   c                 j    SnSU -  S-
  n[        U S-
  5       H  n[        USU5      S-
  nM     US:H  $ )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>   r   r   r   )r1   r)   )pvrQ   r6   s       r$   _lucas_lehmer_primality_testrV     sD    D 	
A	1qA1q5\1aL1 6Mr&   c                     [        U 5      n U S:  a  gX S-   -  (       a  gU R                  5       nU[        ;   a  gUS:  d  [        U5      (       d  g[	        U5      nU(       a  [        [        SU-  5      5      eU$ )aS  Returns 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

r   FTi@z`
            This Mersenne Prime, 2^%s - 1, should
            be added to SymPy's known values.)r   rP   MERSENNE_PRIME_EXPONENTSisprimerV   r(   r   )r"   rT   results      r$   is_mersenne_primer[   .  s    ( 	q	A1uE{	A$$:~WQZZ )!,F %134%5 6 7 	7 Mr&   (   i<  i     i  i  i>  i)  i/  i  i  i
  i$  i0  i8  i\  ia
  i  i  i,  i  ix$  i  i^	  iO  i%(  i\  i  iK  il  i  i  i  iq  i5  iS  i  i2  iW	  i  i  i#  i  i#  i  i  iD  iy  i'  i  i  i,    i    i  i  r^   i	  i  i  i  i-  i)  i  r   i;  i  i:  i  iZ  i  i  i  i6  i  i  i  i  i  i_  i  i`  i     i  iH  i  i6  i     )   iM  i&  i3  i"  i;  i  i  i  i`  i#     i  i  i     i  i
  i  i[)  i.'  iI	  in  i  i_  i  i  ii  i  i1  i  ix  iY  i*  i  i  i:  r_   i}  i  i     i8	  i  i"  iU  i  iR  i!     i
  i
  i  i  i  iv  iT  i-  i\  iJ  iF  i'  i  i  i&  i 8  iC  rb   i_  i"'  iH1  iX&  i[  i  iK  ip*  iY  r]   i  i  i  iD  i  i7  i;  iP  iQ  i  i   i   iX  i#  i  iN  i  i  i"  ia  i  iJ  ip  i6  i*  i  i
  i  i   i  i  iA'  if  i  i	  i  i  ib  i  i  i  i  i  i  iz  i	  ih  i   i`  iI  iY  i	  *   i  i|  r\   i  is  i	  i0  i  iv  I   i  i  i   iM  i4  i  i  i  i+  i  i^  iZ  id  iy  ig
  id  i  iU  i  i  X   iQ,  i  i     c                 <   [        U 5      n U S;   a  gU S:  d  U S-  S:X  d  U S-  S:X  d	  U S-  S:X  a  gU S:  a  gU S	-  S:X  dc  U S
-  S:X  dZ  U S-  S:X  dQ  U S-  S:X  dH  U S-  S:X  d?  U S-  S:X  d6  U S-  S:X  d-  U S-  S:X  d$  U S-  S:X  d  U S-  S:X  d  U S-  S:X  d	  U S-  S:X  a  gU S:  a  gU S:  a$  [        SU S-	  U 5      SU S-
  4;   =(       a    U S;  $ SSKJn  XR                  S   ::  a  UR                  U 5      u  p#X#:H  $ SSKJn  UR                  U 5      =nb  XP:H  $ [        b  [        U 5      $ U S:  a  [        U S/5      $ U S:  a5  U S-	  U -  S -  nUS-	  U-  S -  nUS-	  U-  S!-  n[        U [        U   /5      $ U S":  a  [        U / S#Q5      $ U S$:  a  [        U / S%Q5      $ U S&:  a  [        U / S'Q5      $ U S(:  a  [        U / S)Q5      $ U S*:  a  [        U / S+Q5      $ U S,:  a  [        U / S-Q5      $ U S.:  a  [        U / S/Q5      $ [        U 5      $ )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
)r   r   r   Tr   r   r   r   F1   r      r   r   r         r   %   ra   +   /   i
  i5  r   )i   i{  i/  i  i=  )siever?   )factor_cachei6 l   j_| l   )W1     i;]   l   ?_F)l   |a?bU l   $|3r_ l   (P7 l   k^AH )r   l   @0 l   e7:`V! l   "SV=Q-O	 l   v- )r   l   morl   \^E~8l   )VmAl   vKHu8 l   ~_!@)r   l   \]A l   bY+5l   0Prl   MK	Tl   0KOM l            )r   iE  i$  in  i i= ikl   7y_@I7 )r   r   r   r   rl   r   r   r   rm   rn   r   ro   l   %!HnfW )r   r   r   r   rl   r   r   r   rm   rn   r   ro   ra   )r   r)   sympy.ntheory.generaterr   _listsearchsympy.ntheory.factor_rs   get_gmpyr   r<   _MR_BASES_32)r"   r3   lurs   reths          r$   rY   rY   s  s|   J 	q	A 	I~1uQ1Q1Q12v	Q1}RA!b&Q1r6a-	B1}RA!b&Q1r6a-	B1}RA!b&Q1r6a-4x5y1a1fa QAJ.^1<^3^^ 2GGBKxx{v2""/x
 !!$$ 	6z!)*++:~ 2g]i'2g]i'2g]c!!l1o&''<!VWW>!TUU!dee!wxx!GHH##!ABB$$!EFFJ a  r&   c                 8   [        U 5      n U R                  5       u  p[        USS9n[        USS9nUS:X  a%  [        U5      n[	        U5      =(       a    US-  S:H  $ US:X  a%  [        U5      n[	        U5      =(       a    US-  S:H  $ [	        US-  US-  -   5      $ )zmTest if num is a Gaussian prime number.

References
==========

.. [1] https://oeis.org/wiki/Gaussian_primes
F)strictr   r>   r   r   )r   as_real_imagr   absrY   )numr#   r5   s      r$   is_gaussian_primer   +  s     #,CDAqAqAAvFqz(a!eqj(	
aFqz(a!eqj(1a4!Q$;r&   N)T)'__doc__	itertoolsr   sympy.core.sympifyr   sympy.external.gmpyr   r{   r   r   r   r.   r	   r
   r   r   r   r   sympy.external.ntheoryr   sympy.utilities.miscr   r   rX   r%   r*   r,   r7   r<   rC   rF   rH   rN   rR   rV   r[   r|   rY   r    r&   r$   <module>r      s   
  &5 5 5
 3 3 ,^51p*Z@('T%P%P#&LEP8v&R'TN6n!p r&   