U
    9%eQ                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	 Zd
d Zd$ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )%z
Primality testing

    )igcd)integer_nthroot)sympify)HAS_GMPY)as_int)bitcountc                  G   s   t dd | D S )Nc                 s   s   | ]}t |V  qd S )N)int).0_ r   V/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/ntheory/primetest.py	<genexpr>   s     z_int_tuple.<locals>.<genexpr>)tuple)ir   r   r   
_int_tuple   s    r   c                 C   sz   ddl m} t| |gsdS t| } | d }t||||? | }|dkrLdS || d kr\dS t|d| }|dkrLdS qLdS )af  Returns True if n is prime or an Euler pseudoprime to base b, else False.

    Euler Pseudoprime : In arithmetic, an odd composite integer n is called an
    euler pseudoprime to base a, if a and n are coprime and satisfy the modular
    arithmetic congruence relation :

    a ^ (n-1)/2 = + 1(mod n) or
    a ^ (n-1)/2 = - 1(mod n)

    (where mod refers to the modulo operation).

    Examples
    ========

    >>> from sympy.ntheory.primetest import is_euler_pseudoprime
    >>> is_euler_pseudoprime(2, 5)
    True

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Euler_pseudoprime
    r   trailingF   T   N)sympy.ntheory.factor_r   mrr   pow)nbr   rcr   r   r   is_euler_pseudoprime   s    r   Tc                 C   s   |r$t | } | dk rdS | dkr$dS dd| d@ > @ s8dS | d }d	d|d
 > @ sTdS dd|d > @ shdS dd|d > @ s|dS t| dd S )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.power.integer_nthroot
    r   F)r   r   Tl	   H0!@@ r      iE l    $  c   l   BEa2P	0@\ [   l   Sq pDQBU   r   )r   r   )r   prepmr   r   r   	is_square?   s     r#   c                 C   sd   t ||| }|dks || d kr$dS td|D ]0}t |d| }|| d krP dS |dkr. dS q.dS )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tr   jr   r   r   _test~   s    r)   c                 C   s   ddl m} ddlm} t| } | dk r,dS || d }| |? }|D ]8}|| krX|| ; }|dkrD||}t| |||sD dS qDdS )aG  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   r   )ZZr   Fr   T)r   r   Zsympy.polys.domainsr*   r   r)   )r   basesr   r*   r&   r'   r%   r   r   r   r      s    r   c           	      C   s  || d|  }| dk r t d|dk r0t d|dkr@t d|dkrTtdd|S d}|}|}t|}|dkr|dkr|| |  }|| d |  }|d8 }||d ? d@ rr|| | || ||   }}|d@ r|| 7 }|d@ r|| 7 }|d? |d?  }}qrn~|dkr|dkr|dkr|| |  }|dkrL|| d |  }n|| d |  }d}|d8 }||d ? d@ r|| |||   }}|d@ r|| 7 }|d@ r|| 7 }|d? |d?  }}d}qn|dkr|| |  }|| d|  |  }||9 }|d8 }||d ? d@ rx|| | || ||   }}|d@ rL|| 7 }|d@ r^|| 7 }|d? |d?  }}||9 }|| ; }qt||  ||  |S )	a  Return the modular Lucas sequence (U_k, V_k, Q_k).

    Given a Lucas sequence defined by P, Q, returns the kth values for
    U and V, along with Q^k, all modulo n.  This is intended for use with
    possibly very large values of n and k, where the combinatorial functions
    would be completely unusable.

    The modular Lucas sequences are used in numerous places in number theory,
    especially in the Lucas compositeness tests and the various n + 1 proofs.

    Examples
    ========

    >>> from sympy.ntheory.primetest import _lucas_sequence
    >>> N = 10**2000 + 4561
    >>> sol = U, V, Qk = _lucas_sequence(N, 3, 1, N//2); sol
    (0, 2, 1)

       r   zn must be >= 2r   zk must be >= 0zD must not be zeror   )
ValueErrorr   
_bitlength)	r   PQkDUVQkr   r   r   r   _lucas_sequence   sp    









r7   c                 C   sv   ddl m} d}tt|| }|dkr2|| kr2dS ||| dkrBqb|dkrV| d }q| d }qt|dd| d S )	a  Calculates the Selfridge parameters (D, P, Q) for n.  This is
       method A from page 1401 of Baillie and Wagstaff.

    References
    ==========
    .. [1] "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
           http://mpqs.free.fr/LucasPseudoprimes.pdf
    r   jacobi_symbol   r   r   r   r   r-   r   r,   )sympy.ntheory.residue_ntheoryr9   r   absr   )r   r9   r3   gr   r   r   _lucas_selfridge_params  s    	r?   c                 C   sf   ddl m} d\}}}t|| }|dkr4|| kr4dS ||| dkrDqZ|d7 }|| d }qt|||S )zCalculates the "extra strong" parameters (D, P, Q) for n.

    References
    ==========
    .. [1] OEIS A217719: Extra Strong Lucas Pseudoprimes
           https://oeis.org/A217719
    .. [1] https://en.wikipedia.org/wiki/Lucas_pseudoprime
    r   r8   )   r   r:   r   r;   r-   r,   )r<   r9   r   r   )r   r9   r0   r1   r3   r>   r   r   r   _lucas_extrastrong_params*  s    	

rA   c                 C   st   t | } | dkrdS | dk s(| d dkr,dS t| dr:dS t| \}}}|dkrTdS t| ||| d \}}}|dkS )a   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
    ==========
    - "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
      http://mpqs.free.fr/LucasPseudoprimes.pdf
    - OEIS A217120: Lucas Pseudoprimes
      https://oeis.org/A217120
    - 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   Tr   Fr   )r   r#   r?   r7   )r   r3   r0   r1   r4   r5   r6   r   r   r   is_lucas_prp@  s     
rB   c                 C   s   ddl m} t| } | dkr dS | dk s4| d dkr8dS t| drFdS t| \}}}|dkr`dS || d }| d |? }t| |||\}}}	|dks|dkrdS td|D ]2}
|| d|	  |  }|dkr dS t|	d| }	qd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
    ==========
    - "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
      http://mpqs.free.fr/LucasPseudoprimes.pdf
    - OEIS A217255: Strong Lucas Pseudoprimes
      https://oeis.org/A217255
    - https://en.wikipedia.org/wiki/Lucas_pseudoprime
    - 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   r   r   TFr   )r   r   r   r#   r?   r7   r$   r   r   r   r3   r0   r1   r&   r2   r4   r5   r6   r   r   r   r   is_strong_lucas_prpo  s,    
rD   c                 C   s   ddl m} t| } | dkr dS | dk s4| d dkr8dS t| drFdS t| \}}}|dkr`dS || d }| d |? }t| |||\}}}	|dkr|dks|| d krdS td|D ]"}
|dkr dS || d |  }qdS )at  Extra Strong Lucas compositeness test.  Returns False if n is
    definitely composite, and True if n is a "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 Grantham 2000, 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.

    With these parameters, there are no counterexamples below 2^64 nor any
    known above that range.  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
    ==========
    - "Frobenius Pseudoprimes", Jon Grantham, 2000.
      https://www.ams.org/journals/mcom/2001-70-234/S0025-5718-00-01197-2/
    - OEIS A217719: Extra Strong Lucas Pseudoprimes
      https://oeis.org/A217719
    - 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   r   r   TFr   )r   r   r   r#   rA   r7   r$   rC   r   r   r   is_extra_strong_lucas_prp  s*    -
rE   c                 C   s  zt | } W n tk
r"   Y dS X | dkr0dS | dk s\| d dks\| d dks\| d dkr`dS | dk rldS | d	 dks| d
 dks| d dks| d dks| d dks| d dks| d dks| d dks| d dks| d dks| d dks| d dkrdS | dk r&dS | dk rJtd| | dkoH| dkS ddlm} | |jd kr||| \}}||kS tdkrddlm	}m
} || do|| S | dk rt| dgS | dk rt| ddgS | d k rt| d!d"d#gS | d$k rt| dd%d&d'gS | d(k r,t| dd)d*d+d,gS | d-k rLt| dd.d/d0d1d2gS | d3k rnt| dd4d5d6d7d8d9gS | d:k rt| dddd	d
dddddddgS | d;k rt| dddd	d
ddddddddgS t| dgot| S )<a  
    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(13.0)  # limited precision
    False
    >>> 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 == int(n)
    True
    >>> n = Float(near_int, 20)
    >>> n == int(n)
    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

    References
    ==========
    - https://en.wikipedia.org/wiki/Strong_pseudoprime
    - "Lucas Pseudoprimes", Baillie and Wagstaff, 1980.
      http://mpqs.free.fr/LucasPseudoprimes.pdf
    - https://en.wikipedia.org/wiki/Baillie-PSW_primality_test
    F)r   r@   r:   Tr   r   r@   r:   1                           %   )   +   /   i
  iz  )i  i   i5  i!I  iM  iQ[  )siever-   )is_strong_prpis_strong_selfridge_prpi6 l   j_| i94l   85Pl   p8K0T l   ?_Fl   |a?bU l   $|3r_ l   (P7 l   k^AH l   @0 l   e7:`V! l   "SV=Q-O	 l   v- l   morl   \^E~8l   )VmAl   vKHu8 l   ~_!@l   \]A l   bY+5l   0Prl   MK	Tl   0KOM l            iE  i$  in  i i= ikl   7y_@I7 l   %!HnfW )r   r.   r   Zsympy.ntheory.generaterS   Z_listsearchr   Zgmpy2rT   rU   r   rD   )r   r&   lurT   rU   r   r   r   isprime  st    E,8



















"
$%rY   c                 C   s   t | } |  \}}t|dd}t|dd}|dkrPt|}t|oN|d dkS |dkrtt|}t|or|d dkS t|d |d  S )z}Test if num is a Gaussian prime number.

    References
    ==========

    .. [1] https://oeis.org/wiki/Gaussian_primes
    F)strictr   r,   r@   r   )r   Zas_real_imagr   r=   rY   )numar   r   r   r   is_gaussian_prime  s    	r]   N)T)__doc__Zsympy.core.numbersr   Zsympy.core.powerr   Zsympy.core.sympifyr   Zsympy.external.gmpyr   Zsympy.utilities.miscr   Zmpmath.libmpr   r/   r   r   r#   r)   r   r7   r?   rA   rB   rD   rE   rY   r]   r   r   r   r   <module>   s(   ,
?+T/;I 3