
    gB                        d dl Z d dlZd dlmZ d gdz  Z edd          D ]Zegddez
  z  z  edez  ddedz   z  <   d'dZd'dZ	d Z
d	 Zd
 Zd Ze j        dd         dk    rej        Zej        Znd dlmZ d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd& Z(dS )(    N            c                 ~   | sd S t          | |z	            } | dz  }|rt          |         |z   S d|z   }| dz  } |                                 dz
  }| d|z  k    r||z   S |dk     r| dz  s| dz  } |dz  }| dz  n4|dz	  }| dz  s*| d|z  dz
  z  r|dz  }| d|z  dz
  z  | |z  } ||z  }| dz  *|t          | dz           z   S )N   r   r   i,  )abs_small_trailing
bit_length)xnlow_bytetzps         R/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/external/ntheory.py	bit_scan1r      s1    AFA4xH -x(1,,	AA!GA	AAF{{1u3wwd( 	!GAFA d( 	 Fd( 	Q!|$ a Q!|$ !GAFA	 d( 	
 q4x(((    c                 .    t          | d|z  z   |          S )Nr   )r   )r   r   s     r   	bit_scan0r   1   s    Q!q&\1%%%r   c                    |dk     rt          d          | dk    rdS |dk    rt          |           }| |z	  |fS d}t          | |          \  }}|s|} |dz  }|dk    rk|dz  g}|rc|d         }t          | |          \  }}|s0|dt          |          z  z  }|} |                    |dz             n|                                 |ct          | |          \  }}|| |fS )N   zfactor must be > 1r   )r   r   r      )
ValueErrorr   divmodlenappendpop)r   fbmyrempow_list_fs           r   remover'   5   s&   1uu-...AvvtAvvaLLAvqy	AAq\\FAs 	Qq551vH #b\23 #c(mm++AAOOBE****LLNNN  # 13   a4Kr   c                 ^    t          t          j        t          |                               S )z
Return x!.)intmlibifacr   s    r   	factorialr-   Q   s     tyQ  !!!r   c                 ^    t          t          j        t          |                               S )zInteger square root of x.)r)   r*   isqrtr,   s    r   sqrtr0   V   s     tz#a&&!!"""r   c                     t          j        t          |                     \  }}t          |          t          |          fS )z'Integer square root of x and remainder.r*   sqrtremr)   )r   srs      r   r3   r3   [   s2    <ADAqFFCFFr   r   )   	   reducec                  8    t          t          j        | d          S )zgcd of multiple integers.r   )r9   mathgcdargss    r   r<   r<   k   s    dha(((r   c                  2    d| v rdS t          d | d          S )zlcm of multiple integers.r   c                 8    | |z  t          j        | |          z  S )N)r;   r<   r   r#   s     r   <lambda>zlcm.<locals>.<lambda>t   s    1Q3A#6 r   r   r8   r=   s    r   lcmrC   p   s&    99166a@@@r   c                      | dk     rd|  fS d| fS )Nr   r   r    r   s    r   _signrG   w   s    1uuA2va4Kr   c                 4   | r|s-t          |           pt          |          }|sdS || |z  ||z  fS t          |           \  }} t          |          \  }}d\  }}d\  }}|r-t          | |          \  }	}
||
}} |||	|z  z
  }}|||	|z  z
  }}|-| ||z  ||z  fS )N)r   r   r   )r   r   r   r   )r	   rG   r   )ar!   gx_signy_signr   r5   r#   r4   qcs              r   gcdextrP   }   s     #A #FFc!ff 	91616""aIFAaIFADAqDAq
 a||1!1!ac'1!ac'1	   q6z1v:&&r   c                     | dk     rdS dd| dz  z  z  rdS | dz  }dd|dz  z  z  rdS d	d|d
z  z  z  rdS dd|dz  z  z  rdS t          j        t          |                     d         dk    S )z$Return True if x is a square number.r   Fl	   }{wo^?{~ r      iE l   }}k-[o{?_}c   l   =}:Mv?_ [   l   }s;yU   r2   r   r"   s     r   	is_squarerW      s    1uuu* *Q1s7^< u	F
A"aAFm4 u A!b&M2 u!B-0 u<A"a''r   c                 `    	 t          | d|          S # t          $ r t          d          w xY w)zModular inverse of x modulo m.

    Returns y such that x*y == 1 mod m.

    Uses ``math.pow`` but reproduces the behaviour of ``gmpy2.invert``
    which raises ZeroDivisionError if no inverse exists.
    r   zinvert() no inverse exists)powr   ZeroDivisionErrorrV   s     r   invertr[      sA    >1b!}} > > > <===>s    -c                     |dk    s|dz  st          d          | |z  } | sdS t          | |dz
  dz  |          dk    rdS dS )zLegendre symbol (x / y).

    Following the implementation of gmpy2,
    the error is raised only when y is an even number.
    r   r   zy should be an odd primer   r   )r   rY   rA   s     r   legendrer]      sc     	AvvQUv3444FA q
1q1ulA!##q2r   c                 p   |dk    s|dz  st          d          | |z  } | st          |dk              S |dk    s| dk    rdS t          | |          dk    rdS d}| dk    rU| dz  dk    r$| dk    r| dz  } |dz  dv r| }| dz  dk    r| dk    || }} | dz  |dz  cxk    rdk    rn n| }| |z  } | dk    U|S )	zJacobi symbol (x / y).r   r   z#y should be an odd positive integerr   r   r6   r      r6   )r   r)   r<   )r   r#   js      r   jacobirb      s   AvvQUv>???FA 16{{Avvaq
1ayyA~~q	A
q&&!eqjjQUU!GA1uB !eqjjQUU !1q5AEQA	Q q&& Hr   c                     t          | |          dk    rdS |dk    rdS |dk     r| dk     rdnd}t          |          }t          |          }||z  }|dz  r
| dz  dv r| }|t          | |          z  S )zKronecker symbol (x / y).r   r   r   r   r   r_   )r<   r	   r   rb   )r   r#   signr4   s       r   	kroneckerre      s    
1ayyA~~qAvvqQ1q5522aDAA!A!GA1u Q&u&A,,r   c                    | dk     rt          d          |dk     rt          d          | dv r| dfS |dk    r| dfS |dk    r)t          j        |           \  }}t          |          | fS ||                                 k    rdS 	 t          | d	|z  z  d
z             }nm# t
          $ r` t          j        |           |z  }|dk    r.t          |dz
            }t          d||z
  z  dz             |z  }nt          d|z            }Y nw xY w|dk    r9d|}}	 ||dz
  z  }||dz
  |z  | |z  z   |z  }}t          ||z
            dk     rn3n|}||z  }|| k     r|dz  }||z  }|| k     || k    r|dz  }||z  }|| k    ||| k    fS )Nr   zy must be nonnegativer   zn must be positiverI   Tr   )r   Fg      ?g      ?5   g       @l           r   )	r   r*   r3   r)   r   OverflowErrorr;   log2r	   )	r#   r   r   r$   guessexpshiftxprevr   s	            r   irootrn      s   1uu01111uu-...F{{$wAvv$wAvva31vv3wALLNNx"A1IO$$ " " "ill1n88bMMEcEk*Q.//58EESMME" u}}uq	AE
AAE19q!t+a/1E1u9~~!!		 	1A
a%%	QqD a%% a%%	QqD a%% a1f9s   B   A'D
	D
c                    |dk     rt          d          | dk     rt          d          | dk    rdS | dz  dk    r| dk    S || z  }t          | |          dk    rt          d          t          || dz
  |           dk    S )Nr   z7is_fermat_prp() requires 'a' greater than or equal to 2r   z.is_fermat_prp() requires 'n' be greater than 0Fr   z&is_fermat_prp() requires gcd(n,a) == 1)r   r<   rY   r   rJ   s     r   is_fermat_prprq   )  s    1uuRSSS1uuIJJJAvvu1uzzAvFA
1ayyA~~ABBBq!a%q  r   c                 &   |dk     rt          d          | dk     rt          d          | dk    rdS | dz  dk    r| dk    S || z  }t          | |          dk    rt          d          t          || dz	  |           t          ||           | z  k    S )Nr   z6is_euler_prp() requires 'a' greater than or equal to 2r   z-is_euler_prp() requires 'n' be greater than 0Fr   z%is_euler_prp() requires gcd(n,a) == 1)r   r<   rY   rb   rp   s     r   is_euler_prprs   8  s    1uuQRRR1uuHIIIAvvu1uzzAvFA
1ayyA~~@AAAq!q&!q!q 000r   c                     t          | dz
            }t          || |z	  |           }|dk    s	|| dz
  k    rdS t          |dz
            D ](}t          |d|           }|| dz
  k    r dS |dk    r dS )dS )Nr   Tr   F)r   rY   range)r   rJ   r4   _s       r   _is_strong_prprw   G  s    !a%AAqAvqAAvva!et1q5\\  1aLLA::446655 5r   c                     |dk     rt          d          | dk     rt          d          | dk    rdS | dz  dk    r| dk    S || z  }t          | |          dk    rt          d          t          | |          S )Nr   z7is_strong_prp() requires 'a' greater than or equal to 2r   z.is_strong_prp() requires 'n' be greater than 0Fr   z&is_strong_prp() requires gcd(n,a) == 1)r   r<   rw   rp   s     r   is_strong_prpry   U  s    1uuRSSS1uuIJJJAvvu1uzzAvFA
1ayyA~~ABBB!Qr   c                    |dk    rdS |dz  d|z  z
  }d}|}|| z  }|dk    rft          |          dd         D ]L}||z  | z  }||z  dz
  | z  }|dk    r1||z  |z   ||z  ||z  z   }}|dz  r|| z  }|dz  r|| z  }|dz	  |dz	  }}Mn\|dk    rs|d	k    rmt          |          dd         D ]O}||z  | z  }|dk    r||z  dz
  | z  }n||z  dz   | z  }d}|dk    r ||z   |dz  }}|dz  r|| z  }|dz  }||z  }d	}P|| z  }n|dk    rgt          |          dd         D ]N}||z  | z  }||z  d|z  z
  | z  }||z  }|dk    r&||z   ||z  dz  }}|dz  r|| z  }|dz  }||z
  }||z  }|| z  }Onvt          |          dd         D ]^}||z  | z  }||z  d|z  z
  | z  }||z  }|dk    r6||z  |z   ||z  ||z  z   }}|dz  r|| z  }|dz  r|| z  }|dz	  |dz	  }}||z  }|| z  }_|| z  || z  |fS )
a  Return the modular Lucas sequence (U_k, V_k, Q_k).

    Explanation
    ===========

    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.

    .. math ::
        U_k = \begin{cases}
             0 & \text{if } k = 0\\
             1 & \text{if } k = 1\\
             PU_{k-1} - QU_{k-2} & \text{if } k > 1
        \end{cases}\\
        V_k = \begin{cases}
             2 & \text{if } k = 0\\
             P & \text{if } k = 1\\
             PV_{k-1} - QV_{k-2} & \text{if } k > 1
        \end{cases}

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

    Parameters
    ==========

    n : int
        n is an odd number greater than or equal to 3
    P : int
    Q : int
        D determined by D = P**2 - 4*Q is non-zero
    k : int
        k is a nonnegative integer

    Returns
    =======

    U, V, Qk : (int, int, int)
        `(U_k \bmod{n}, V_k \bmod{n}, Q^k \bmod{n})`

    Examples
    ========

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

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Lucas_sequence

    r   )r   r   r   r   r`   r   r6   N1r   )bin)	r   PQkDUVQkr!   s	            r   _lucas_sequencer   d  s,   r 	Avvy	1qs
A	A	A	
QBAvvQ 		& 		&A1	A1qAACxxsQw!ac	1q5 FAq5 FAAvqAv1		& 
aAGGQ 	 	A1	AQwwqS1WMqS1WMCxx AqAv1q5 FAaQ
a	
aQ 	 	A1	A1qtq A"HBCxx A!z1q5 FAaEa!GBB	  Q 	 	A1	A1qtq A"HBCxxsQw!ac	1q5 FAq5 FAAvqAv1a!GBBE1q5"r   c                     |dz  d|z  z
  }|dk    s
|dk    s|dvrt          d          | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          | |||           d         || z  k    S )	Nr   r`   r   )r   r   z,invalid values for p,q in is_fibonacci_prp()r   z1is_fibonacci_prp() requires 'n' be greater than 0F)r   r   r   r   rN   ds       r   is_fibonacci_prpr     s    	1qs
AAvva1G++GHHH1uuLMMMAvvu1uzzAv1aA&&q)QU22r   c           
      @   |dz  d|z  z
  }|dk    rt          d          | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          | ||z            d| fvrt          d          t          | ||| t          ||           z
            d         dk    S )	Nr   r`   r   z(invalid values for p,q in is_lucas_prp()r   z-is_lucas_prp() requires 'n' be greater than 0Fz)is_lucas_prp() requires gcd(n,2*q*D) == 1)r   r<   r   rb   r   s       r   is_lucas_prpr     s    	1qs
AAvvCDDD1uuHIIIAvvu1uzzAv
1ac{{1a&  DEEE1aAq!$455a8A==r   c                    t          ddd          D ]m}|dz  r| }t          ||           }|dk    r't          | dd|z
  dz  | dz             d         dk    c S |dk    r|| z  r dS |d	k    rt          |           r dS nt	          d
          )ad  Lucas compositeness test with the Selfridge parameters for n.

    Explanation
    ===========

    The Lucas compositeness test checks whether n is a prime number.
    The test can be run with arbitrary parameters ``P`` and ``Q``, which also change the performance of the test.
    So, which parameters are most effective for running the Lucas compositeness test?
    As an algorithm for determining ``P`` and ``Q``, Selfridge proposed method A [1]_ page 1401
    (Since two methods were proposed, referred to simply as A and B in the paper,
    we will refer to one of them as "method A").

    method A fixes ``P = 1``. Then, ``D`` defined by ``D = P**2 - 4Q`` is varied from 5, -7, 9, -11, 13, and so on,
    with the first ``D`` being ``jacobi(D, n) == -1``. Once ``D`` is determined,
    ``Q`` is determined to be ``(P**2 - D)//4``.

    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

    r   @B r   r   r   r`   r   F   z=appropriate value for D cannot be found in is_selfridge_prp())ru   rb   r   rW   r   )r   r   ra   s      r   _is_selfridge_prpr     s    4 1i## 
 
q5 	A1aLL77"1a!A#!QU;;A>!CCCC66a!e65577y||755
T
U
UUr   c                 x    | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          |           S )Nr   1is_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   r   rF   s    r   is_selfridge_prpr   '  sL    1uuLMMMAvvu1uzzAvQr   c                    |dz  d|z  z
  }|dk    rt          d          | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          | ||z            d| fvrt          d          t          ||           }t          | |z
            }t	          | ||| |z
  |z	            \  }}}|dk    s|dk    rd	S t          |dz
            D ]*}	||z  d|z  z
  | z  }|dk    r d	S t          |d|           }+dS )
Nr   r`   r   z/invalid values for p,q in is_strong_lucas_prp()r   r   Fz0is_strong_lucas_prp() requires gcd(n,2*q*D) == 1T)r   r<   rb   r   r   ru   rY   )
r   r   rN   r   ra   r4   r   r   r   rv   s
             r   is_strong_lucas_prpr   1  s;   	1qs
AAvvJKKK1uuLMMMAvvu1uzzAv
1ac{{1a&  KLLLq!A!a%Aq!QQ155HAq"Avvat1q5\\  qS1R4Z16644Q]]5r   c                    t          ddd          D ]}|dz  r| }t          ||           }|dk    rt          | dz             }t          | dd|z
  dz  | dz   |z	            \  }}}|dk    s|dk    r dS t          |dz
            D ]+}||z  d|z  z
  | z  }|dk    r  dS t	          |d|           }, d	S |dk    r|| z  r d	S |d
k    rt          |           r d	S t          d          )Nr   r   r   r   r   r`   r   TFr   zDappropriate value for D cannot be found in is_strong_selfridge_prp())ru   rb   r   r   rY   rW   r   )r   r   ra   r4   r   r   r   rv   s           r   _is_strong_selfridge_prpr   J  s3   1i##  q5 	A1aLL77!a%  A&q!acaZ!a%AFFHAq"Avvatt1q5\\ # #qS1R4Z1$66444Q]]5566a!e65577y||755
[
\
\\r   c                 x    | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          |           S )Nr   z8is_strong_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   r   rF   s    r   is_strong_selfridge_prpr   b  sL    1uuSTTTAvvu1uzzAv#A&&&r   c                     | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          | d          ot          |           S )Nr   z,is_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   rw   r   rF   s    r   is_bpsw_prpr   l  s\    1uuGHHHAvvu1uzzAv!Q8$5a$8$88r   c                     | dk     rt          d          | dk    rdS | dz  dk    r| dk    S t          | d          ot          |           S )Nr   z3is_strong_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   rw   r   rF   s    r   is_strong_bpsw_prpr   v  s\    1uuNOOOAvvu1uzzAv!Q?$<Q$?$??r   )r   ))sysr;   mpmath.libmplibmpr*   r
   ru   ra   r   r   r'   r-   r0   r3   version_infor<   rC   	functoolsr9   rG   rP   rW   r[   r]   rb   re   rn   rq   rs   rw   ry   r   r   r   r   r   r   r   r   r   r   rE   r   r   <module>r      s   


        #)	q! C CA/0cQ1q5\.BOAF*aAEl*++) ) ) )@& & & &  8" " "
# # #
   BQB6!!
(C
(CC !     ) ) )
A A A  ' ' '*!( !( !(H> > >     0  + + +\! ! !1 1 1       | | |~
3 
3 
3> > >%V %V %VP       2] ] ]0' ' '9 9 9@ @ @ @ @r   