
    \h@                     Z   S SK r S SKJr  S /S-  r\" SS5       H  r\/SS\-
  -  -  \S\-  SS\S-   -  2'   M      S"S jrS"S jrS r	S	 r
S
 rS r\ R                  r\ R                  rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!S r"S  r#S! r$g)#    N            c                    U (       d  g [        X-	  5      n U S-  nU(       a  [        U   U-   $ SU-   nU S-  n U R                  5       S-
  nU SU-  :X  a  XC-   $ US:  a!  U S-  (       d  U S-  n US-  nU S-  (       d  M  OJUS-	  nU S-  (       d;  U SU-  S-
  -  (       a  US-  nU SU-  S-
  -  (       a  M  X-  n X5-  nU S-  (       d  M;  U[        U S-     -   $ )N   r   r   i,  )abs_small_trailing
bit_length)xnlow_bytetzps         N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/external/ntheory.py	bit_scan1r      s    AFA4xHx(1,,	AA!GA	AAF{u3wd(!GAFA d(( Fd(Q!|$a Q!|$$GAFA	 d((
 q4x(((    c                 &    [        U SU-  -   U5      $ )Nr   )r   )r   r   s     r   	bit_scan0r   0   s    Q!q&\1%%r   c                    US:  a  [        S5      eU S:X  a  gUS:X  a  [        U 5      nX-	  U4$ Sn[        X5      u  pEU(       d  Un US-  nUS:  ag  US-  /nU(       aZ  US   n[        X5      u  pEU(       d(  US[        U5      -  -  nUn UR	                  US-  5        OUR                  5         U(       a  MZ  [        X5      u  pEU(       d  M  X4$ )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'   4   s    1u-..AvAvaLvqy	AA\FA	Qq51vHb\c(m++AAOOBE*LLN (  c 4Kr   c                 R    [        [        R                  " [        U 5      5      5      $ )z
Return x!.)intmlibifacr   s    r   	factorialr-   P   s    tyyQ !!r   c                 R    [        [        R                  " [        U 5      5      5      $ )zInteger square root of x.)r)   r*   isqrtr,   s    r   sqrtr0   U   s    tzz#a&!""r   c                 p    [         R                  " [        U 5      5      u  p[        U5      [        U5      4$ )z'Integer square root of x and remainder.r*   sqrtremr)   )r   srs      r   r3   r3   Z   s)    <<ADAFCFr   c                      U S:  a  SU * 4$ SU 4$ )Nr   r   r    r   s    r   _signr9   d   s    1uA2va4Kr   c                 *   U (       a  U(       d.  [        U 5      =(       d    [        U5      nU(       d  gX U-  X-  4$ [        U 5      u  p0[        U5      u  pASu  pVSu  pxU(       a&  [        X5      u  pXpXeX-  -
  peXX-  -
  pU(       a  M&  XU-  Xt-  4$ )N)r   r   r   )r   r   r   r   )r	   r9   r   )ar!   gx_signy_signr   r5   r#   r4   qcs              r   gcdextrB   j   s    AFc!f616""aIFaIFDADA
a|1ac'1ac'1	 ! 6z1:&&r   c                     U S:  a  gSSU S-  -  -  (       a  gU S-  nSSUS-  -  -  (       a  gS	SUS
-  -  -  (       a  gSSUS-  -  -  (       a  g[         R                  " [        U 5      5      S   S:H  $ )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_squarerI      s    1u* *Q1s7^<	F
A"aAFm4 A!b&M2!B-0<<A"a''r   c                 P     [        U SU5      $ ! [         a    [        S5      ef = f)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   ZeroDivisionErrorrH   s     r   invertrM      s0    >1b!} > <==>s    %c                     US::  d
  US-  (       d  [        S5      eX-  n U (       d  g[        XS-
  S-  U5      S:X  a  gg)ztLegendre 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   rK   )r   r#   s     r   legendrerO      sG     	AvQU344FA
11ulA!#r   c                 h   US::  d
  US-  (       d  [        S5      eX-  n U (       d  [        US:H  5      $ US:X  d  U S:X  a  g[        X5      S:w  a  gSnU S:w  aX  U S-  S:X  a(  U S:  a"  U S-  n US-  S;   a  U* nU S-  S:X  a  U S:  a  M"  XpU S-  US-  s=:X  a  S:X  a  O  OU* nX-  n U S:w  a  MX  U$ )	zJacobi symbol (x / y).r   r   z#y should be an odd positive integerr   r      r      rR   )r   r)   gcd)r   r#   js      r   jacobirV      s    AvQU>??FA16{Ava
1yA~	A
q&!eqjQU!GA1uB !eqjQU 1q5AEQA	 q& Hr   c                     [        X5      S:w  a  gUS:X  a  gUS:  a  U S:  a  SOSn[        U5      n[        U5      nX-  nUS-  (       a  U S-  S;   a  U* nU[        X5      -  $ )zKronecker symbol (x / y).r   r   r   r   r   rQ   )rT   r	   r   rV   )r   r#   signr4   s       r   	kroneckerrY      sm    
1yA~AvQ1q52aDAA!AGA1uQ&u&,r   c                    U S:  a  [        S5      eUS:  a  [        S5      eU S;   a  U S4$ US:X  a  U S4$ US:X  a*  [        R                  " U 5      u  p#[        U5      U(       + 4$ XR	                  5       :  a  g [        U S	U-  -  S
-   5      nUS:  a/  SUp' X!S-
  -  nX!S-
  U-  X-  -   U-  p'[        X'-
  5      S:  a  OM+  UnX!-  nX:  a  US-  nX!-  nX:  a  M  X:  a  US-  nX!-  nX:  a  M  X(U :H  4$ ! [
         aV    [        R                  " U 5      U-  nUS:  a&  [        US-
  5      n[        SXV-
  -  S-   5      U-  n N[        SU-  5      n Nf = f)Nr   zy must be nonnegativer   zn must be positiver;   Tr   )r   Fg      ?g      ?5   g       @l           r   )	r   r*   r3   r)   r   OverflowErrormathlog2r	   )	r#   r   r   r$   guessexpshiftxprevr   s	            r   irootrc      s   1u0111u-..F{$wAv$wAva1v3wLLN"A1IO$ u}uqE
AE19qt+a/119~!	  	A
%	QD % %	QD % 1f93  "iil1n8bMEck*Q./58ESME"s   <C; ;AE
EEc                     US:  a  [        S5      eU S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ X-  n[        X5      S:w  a  [        S5      e[        XS-
  U 5      S:H  $ )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   rT   rK   r   r<   s     r   is_fermat_prprf     sz    1uRSS1uIJJAv1uzAvFA
1yA~ABBqa%q  r   c                     US:  a  [        S5      eU S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ X-  n[        X5      S:w  a  [        S5      e[        XS-	  U 5      [        X5      U -  :H  $ )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   rT   rK   rV   re   s     r   is_euler_prprh   %  s    1uQRR1uHIIAv1uzAvFA
1yA~@AAqq&!qq 000r   c                     [        U S-
  5      n[        XU-	  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)Nr   Tr   F)r   rK   range)r   r<   r4   _s       r   _is_strong_prprl   4  sl    !a%AAAvqAAv!e1q5\1aLA:6  r   c                     US:  a  [        S5      eU S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ X-  n[        X5      S:w  a  [        S5      e[        X5      $ )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   rT   rl   re   s     r   is_strong_prprn   B  so    1uRSS1uIJJAv1uzAvFA
1yA~ABB!r   c                    US:X  a  gUS-  SU-  -
  nSnUnX -  nUS:X  ad  [        U5      SS  HP  nXV-  U -  nXf-  S-
  U -  nUS:X  d  M  XQ-  U-   Xa-  XT-  -   peUS-  (       a  XP-  nUS-  (       a  X`-  nUS-	  US-	  peMR     GOEUS:X  am  US	:X  ag  [        U5      SS  HP  nXV-  U -  nUS:X  a  Xf-  S-
  U -  nOXf-  S-   U -  nSnUS:X  d  M/  XV-   US-  peUS-  (       a  XP-  nUS-  nXe-  nS	nMR     Xp-  nOUS:X  a]  [        U5      SS  HJ  nXV-  U -  nXf-  SU-  -
  U -  nXw-  nUS:X  a%  XV-   X%-  S-  peUS-  (       a  XP-  nUS-  nXV-
  nXr-  nXp-  nML     Oo[        U5      SS  H]  nXV-  U -  nXf-  SU-  -
  U -  nXw-  nUS:X  a8  XQ-  U-   Xa-  XT-  -   peUS-  (       a  XP-  nUS-  (       a  X`-  nUS-	  US-	  peXr-  nXp-  nM_     XP-  X`-  U4$ )
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   rS   r   rR   N1r   )bin)	r   PQkDUVQkr!   s	            r   _lucas_sequencery   Q  sp   r 	Av	1qs
A	A	A	
BAvQA	AqAACxsQwac	1q5FAq5FAAvqAv1  
aAGQA	AQwS1WMS1WMCx qAv1q5FAa   		
aQA	Aqtq AHBCx z1q5FAaEGB   QA	Aqtq AHBCxsQwac	1q5FAq5FAAvqAv1GB  E15"r   c                     US-  SU-  -
  nUS:X  d  US::  d  US;  a  [        S5      eU S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        XX 5      S   X-  :H  $ )	Nr   rS   r   )r   r   z,invalid values for p,q in is_fibonacci_prp()r   z1is_fibonacci_prp() requires 'n' be greater than 0F)r   ry   r   r   r@   ds       r   is_fibonacci_prpr}     s}    	1qs
AAva1G+GHH1uLMMAv1uzAv1&q)QU22r   c           
          US-  SU-  -
  nUS:X  a  [        S5      eU S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        XU-  5      SU 4;  a  [        S5      e[        XX [        X05      -
  5      S   S:H  $ )	Nr   rS   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   rT   ry   rV   r{   s       r   is_lucas_prpr     s    	1qs
AAvCDD1uHIIAv1uzAv
1c{1a& DEE1q$45a8A==r   c                    [        SSS5       Hk  nUS-  (       a  U* n[        X5      nUS:X  a  [        U SSU-
  S-  U S-   5      S   S:H  s  $ US:X  a  X-  (       a    gUS	:X  d  MY  [        U 5      (       d  Mk    g   [	        S
5      e)a  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   rS   r   F   z=appropriate value for D cannot be found in is_selfridge_prp())rj   rV   ry   rI   r   )r   ru   rU   s      r   _is_selfridge_prpr     s    4 1i#q5A1L7"1a!A#!QU;A>!CC6ae7y|| $ T
UUr   c                 d    U S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        U 5      $ )Nr   1is_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   r   r8   s    r   is_selfridge_prpr     s>    1uLMMAv1uzAvQr   c                    US-  SU-  -
  nUS:X  a  [        S5      eU S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        XU-  5      SU 4;  a  [        S5      e[        X05      n[        X-
  5      n[	        XX U-
  U-	  5      u  pgnUS:X  d  US:X  a  g	[        US-
  5       H%  n	Xw-  SU-  -
  U -  nUS:X  a    g	[        USU 5      nM'     g)
Nr   rS   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   rT   rV   r   ry   rj   rK   )
r   r   r@   ru   rU   r4   rv   rw   rx   rk   s
             r   is_strong_lucas_prpr     s    	1qs
AAvJKK1uLMMAv1uzAv
1c{1a& KLLqA!%AqQQ15HA"Ava1q5\S1R4Z16Q]	 
 r   c                    [        SSS5       H  nUS-  (       a  U* n[        X5      nUS:X  as  [        U S-   5      n[        U SSU-
  S-  U S-   U-	  5      u  pEnUS:X  d  US:X  a    g[        US-
  5       H&  nXU-  SU-  -
  U -  nUS:X  a      g[	        USU 5      nM(       g	US:X  a  X-  (       a    g	US
:X  d  M  [        U 5      (       d  M    g	   [        S5      e)Nr   r   r   r   r   rS   r   TFr   zDappropriate value for D cannot be found in is_strong_selfridge_prp())rj   rV   r   ry   rK   rI   r   )r   ru   rU   r4   rv   rw   rx   rk   s           r   _is_strong_selfridge_prpr   7  s    1i#q5A1L7!a% A&q!acaZ!a%AFHA"Ava1q5\S1R4Z1$6Q]	 "
 6ae7y||' $( [
\\r   c                 d    U S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        U 5      $ )Nr   z8is_strong_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   r   r8   s    r   is_strong_selfridge_prpr   O  s>    1uSTTAv1uzAv#A&&r   c                     U S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        U S5      =(       a    [        U 5      $ )Nr   z,is_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   rl   r   r8   s    r   is_bpsw_prpr   Y  sK    1uGHHAv1uzAv!Q8$5a$88r   c                     U S:  a  [        S5      eU S:X  a  gU S-  S:X  a  U S:H  $ [        U S5      =(       a    [        U 5      $ )Nr   z3is_strong_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   rl   r   r8   s    r   is_strong_bpsw_prpr   c  sK    1uNOOAv1uzAv!Q?$<Q$??r   )r   )%r]   mpmath.libmplibmpr*   r
   rj   rU   r   r   r'   r-   r0   r3   rT   lcmr9   rB   rI   rM   rO   rV   rY   rc   rf   rh   rl   rn   ry   r}   r   r   r   r   r   r   r   r   r7   r   r   <module>r      s      #)	q!A/0cQ1q5\.BOAF*aAEl*+ 
)@&8"
#
 hh
hh'*!(H> 0+\!1 |~
3>%VP 2]0'9@r   