+
    i@                        ^ RI t ^ RIHt ^ .R,          t]! ^^4       F6  t].^^],
          ,          ,          ]^],          R^]^,           ,          1&   K8  	  RR ltRR ltR t	R t
R tR t] P                  t] P                  tR	 tR
 tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR t R t!R t"R t#R t$R# )     N   c                 0   V '       g   R # \        W,	          4      p V ^,          pV'       d   \        V,          V,           # ^V,           pV ^,          p V P                  4       ^,
          pV ^V,          8X  d	   WC,           # VR8  d%   V ^,          '       g   V ^,          p V^,          pK#  MRV^,	          pV ^,          '       g;   V ^V,          ^,
          ,          '       d   V^,          pK(  W,          p W5,          pKI  V\        V ^,          ,          ,           # )Ni,  )abs_small_trailing
bit_length)xnlow_bytetzps   &&    v/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/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  Fd((Q!|$$aGAFAq4x(((    c                 6    \        V ^V,          ,           V4      # )   )r   )r   r	   s   &&r   	bit_scan0r   0   s    Q!q&\1%%r   c                    V^8  d   \        R4      hV ^ 8X  d   R# V^8X  d   \        V 4      pW,	          V3# ^ p\        W4      w  rEV'       g   Tp V^,          pV^8  dx   V^,          .pV'       df   VR,          p\        W4      w  rEV'       g6   V^\        V4      ,          ,          pTp VP	                  V^,          4       K[  VP                  4        Km  \        W4      w  rEK  W3# )   zfactor must be > 1)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34Kr   c                R    \        \        P                  ! \        V 4      4      4      # )z
Return x!.)intmlibifacr   s   &r   	factorialr)   P   s    tyyQ !!r   c                R    \        \        P                  ! \        V 4      4      4      # )zInteger square root of x.)r%   r&   isqrtr(   s   &r   sqrtr,   U   s    tzz#a&!""r   c                p    \         P                  ! \        V 4      4      w  r\        V4      \        V4      3# )z'Integer square root of x and remainder.r&   sqrtremr%   )r   srs   &  r   r/   r/   Z   s)    <<ADAFCFr   c                 "    V ^ 8  d   RV ) 3# ^V 3# )r   r    r	   s   &r   _signr5   d   s    1uA2va4Kr   c                 d   V '       d	   V'       g:   \        V 4      ;'       g    \        V4      pV'       g   R# W V,          W,          3# \        V 4      w  r0\        V4      w  rA^^ re^ ^rV'       d0   \        W4      w  rYrYeW,          ,
          reYW,          ,
          rK7  WV,          Wt,          3# )r   )r   r   r   )r   r5   r   )ar   gx_signy_signr   r1   r   r0   qcs   &&         r   gcdextr=   j   s    AFc!f616""aIFaIFaqaq
a|1ac'1ac'16z1:&&r   c                p   V ^ 8  d   R# R^V ^,          ,          ,          '       d   R# V R,          pR^V^c,          ,          ,          '       d   R# R^V^[,          ,          ,          '       d   R# R^V^U,          ,          ,          '       d   R# \         P                  ! \        V 4      4      ^,          ^ 8H  # )z$Return True if x is a square number.Fl	   }{wo^?{~ iE l   }}k-[o{?_}l   =}:Mv?_ l   }s;yr.   r   r   s   & r   	is_squarer@      s    1u* *Q1s7^<<	F
A"aAFm44 A!b&M22!B-00<<A"a''r   c                R     \        V RV4      #   \         d    \        R4      hi ; i)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.
zinvert() no inverse existsr   )powr   ZeroDivisionErrorr?   s   &&r   invertrD      s0    >1b!} > <==>s    &c                    V^ 8:  g   V^,          '       g   \        R4      hW,          p V '       g   ^ # \        W^,
          ^,          V4      ^8X  d   ^# R# )ztLegendre symbol (x / y).

Following the implementation of gmpy2,
the error is raised only when y is an even number.
zy should be an odd primer   )r   rB   )r   r   s   &&r   legendrerF      sK     	AvQUU344FA
11ulA!#Ir   c                   V^ 8:  g   V^,          '       g   \        R4      hW,          p V '       g   \        V^8H  4      # V^8X  g   V ^8X  d   ^# \        W4      ^8w  d   ^ # ^pV ^ 8w  dc   V ^,          ^ 8X  d&   V ^ 8  d   V ^,          p V^,          R9   g   K.  V) pK3  YrV ^,          V^,          u;8X  d   ^8X  d   M MV) pW,          p Ki  V# )zJacobi symbol (x / y).z#y should be an odd positive integer      )r   r%   gcd)r   r   js   && r   jacobirM      s    AvQUU>??FA16{Ava
1yA~	A
q&!eqjQU!GA1uB1q5AEQA	Hr   c                    \        W4      ^8w  d   ^ # V^ 8X  d   ^# V^ 8  d
   V ^ 8  d   RM^p\        V4      p\        V4      pW,          pV^,          '       d   V ^,          R9   d   V) pV\        W4      ,          # )zKronecker symbol (x / y).r   rH   )rK   r   r   rM   )r   r   signr0   s   &&  r   	kroneckerrP      so    
1yA~AvQ1q52aDAA!AGA1uuQ&u&,r   c                 ~   V ^ 8  d   \        R4      hV^8  d   \        R4      hV R9   d   V R3# V^8X  d   V R3# V^8X  d+   \        P                  ! V 4      w  r#\        V4      V'       * 3# WP	                  4       8  d   R#  \        V RV,          ,          R,           4      pVR	8  dQ   R
Tr' W!^,
          ,          pY!^,
          V,          W,          ,           V,          r'\        W',
          4      ^8  g   KL  MTpW!,          pW8  d   V^,          pW!,          pK  W8  d   V^,          pW!,          pK  W(V 8H  3#   \
         dv    \        P                  ! T 4      T,          pT^58  d<   \        T^5,
          4      p\        RYV,
          ,          ^,           4      T,          p EL\        RT,          4      p ELi ; i)r   zy must be nonnegativezn must be positiveTg      ?g      ?g       @)r   r   )r   Fl           r   )	r   r&   r/   r%   r   OverflowErrormathlog2r   )	r   r	   r   r    guessexpshiftxprevr   s	   &&       r   irootrY      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    D< <A'F<&F<;F<c                     V^8  d   \        R4      hV ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # W,          p\        W4      ^8w  d   \        R4      h\        W^,
          V 4      ^8H  # )r   z7is_fermat_prp() requires 'a' greater than or equal to 2z.is_fermat_prp() requires 'n' be greater than 0Fz&is_fermat_prp() requires gcd(n,a) == 1)r   rK   rB   r	   r7   s   &&r   is_fermat_prpr\     sz    1uRSS1uIJJAv1uzAvFA
1yA~ABBqa%q  r   c                    V^8  d   \        R4      hV ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # W,          p\        W4      ^8w  d   \        R4      h\        W^,	          V 4      \        W4      V ,          8H  # )r   z6is_euler_prp() requires 'a' greater than or equal to 2z-is_euler_prp() requires 'n' be greater than 0Fz%is_euler_prp() requires gcd(n,a) == 1)r   rK   rB   rM   r[   s   &&r   is_euler_prpr^   %  s    1uQRR1uHIIAv1uzAvFA
1yA~@AAqq&!qq 000r   c                     \        V ^,
          4      p\        WV,	          V 4      pV^8X  g   W^,
          8X  d   R# \        V^,
          4       F*  p\        V^V 4      pW^,
          8X  d    R# V^8X  g   K)   R# 	  R# )r   TF)r   rB   range)r	   r7   r0   _s   &&  r   _is_strong_prprb   4  sl    !a%AAAvqAAv!e1q5\1aLA:6  r   c                     V^8  d   \        R4      hV ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # W,          p\        W4      ^8w  d   \        R4      h\        W4      # )r   z7is_strong_prp() requires 'a' greater than or equal to 2z.is_strong_prp() requires 'n' be greater than 0Fz&is_strong_prp() requires gcd(n,a) == 1)r   rK   rb   r[   s   &&r   is_strong_prprd   B  so    1uRSS1uIJJAv1uzAvFA
1yA~ABB!r   c                2   V^ 8X  d   R# V^,          ^V,          ,
          p^pTpW ,          pV^8X  d   \        V4      R,           F  pWV,          V ,          pWf,          ^,
          V ,          pVR8X  g   K1  WQ,          V,           Wa,          WT,          ,           reV^,          '       d	   WP,          pV^,          '       d	   W`,          pV^,	          V^,	          reK  	  EM)V^8X  d   VR8X  d   \        V4      R,           F  pWV,          V ,          pV^8X  d   Wf,          ^,
          V ,          pMWf,          ^,           V ,          p^pVR8X  g   KQ  WV,           V^,          reV^,          '       d	   WP,          pV^,          pWe,          pRpK  	  Wp,          pEMpV^8X  d   \        V4      R,           F  pWV,          V ,          pWf,          ^V,          ,
          V ,          pWw,          pVR8X  dG   WV,           W%,          ^,          reV^,          '       d	   WP,          pV^,          pWV,
          pWr,          pWp,          pK  	  M\        V4      R,           F  pWV,          V ,          pWf,          ^V,          ,
          V ,          pWw,          pVR8X  dk   WQ,          V,           Wa,          WT,          ,           reV^,          '       d	   WP,          pV^,          '       d	   W`,          pV^,	          V^,	          reWr,          pWp,          pK  	  WP,          W`,          V3# )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

:rI   NN1)r   r   r   r   )bin)	r	   PQkDUVQkr   s	   &&&&     r   _lucas_sequencero   Q  s   r 	Av	1qs
A	A	A	
BAvQA	AqAACxsQwac	1q55FAq55FAAvqAv1  
aAGQA	AQwS1WMS1WMCx qAv1q55FAa   		
aQA	Aqtq AHBCx z1q55FAaEGB   QA	Aqtq AHBCxsQwac	1q55FAq55FAAvqAv1GB  E15"r   c                    V^,          ^V,          ,
          pV^ 8X  g   V^ 8:  g   VR9  d   \        R4      hV ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        WW 4      ^,          W,          8H  # )r   z,invalid values for p,q in is_fibonacci_prp()z1is_fibonacci_prp() requires 'n' be greater than 0F)r   r   )r   ro   r	   r   r;   ds   &&& r   is_fibonacci_prprs     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           
      D   V^,          ^V,          ,
          pV^ 8X  d   \        R4      hV ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        WV,          4      ^V 39  d   \        R4      h\        WW \        W04      ,
          4      ^ ,          ^ 8H  # )r   z(invalid values for p,q in is_lucas_prp()z-is_lucas_prp() requires 'n' be greater than 0Fz)is_lucas_prp() requires gcd(n,2*q*D) == 1)r   rK   ro   rM   rq   s   &&& r   is_lucas_prpru     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                P   \        ^R^4       F  pV^,          '       d   V) p\        W4      pVR8X  d0   \        V ^^V,
          ^,          V ^,           4      ^ ,          ^ 8H  u # V^ 8X  d   W,          '       d    R# V^8X  g   Kw  \        V 4      '       g   K   R# 	  \	        R4      h)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

@B Fz=appropriate value for D cannot be found in is_selfridge_prp()r   )r`   rM   ro   r@   r   )r	   rk   rL   s   &  r   _is_selfridge_prprx     s    4 1i#q55A1L7"1a!A#!QU;A>!CC6aee7y|| $ T
UUr   c                 t    V ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        V 4      # )r   1is_selfridge_prp() requires 'n' be greater than 0F)r   rx   r4   s   &r   is_selfridge_prpr{     s>    1uLMMAv1uzAvQr   c                 (   V^,          ^V,          ,
          pV^ 8X  d   \        R4      hV ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        WV,          4      ^V 39  d   \        R4      h\        W04      p\        W,
          4      p\	        WW V,
          V,	          4      w  rgpV^ 8X  g   V^ 8X  d   R# \        V^,
          4       F7  p	Ww,          ^V,          ,
          V ,          pV^ 8X  d    R# \        V^V 4      pK9  	  R# )r   z/invalid values for p,q in is_strong_lucas_prp()rz   Fz0is_strong_lucas_prp() requires gcd(n,2*q*D) == 1T)r   rK   rM   r   ro   r`   rB   )
r	   r   r;   rk   rL   r0   rl   rm   rn   ra   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                 4   \        ^R^4       F  pV^,          '       d   V) p\        W4      pVR8X  d   \        V ^,           4      p\        V ^^V,
          ^,          V ^,           V,	          4      w  rEpV^ 8X  g   V^ 8X  d    R# \        V^,
          4       F8  pWU,          ^V,          ,
          V ,          pV^ 8X  d     R# \	        V^V 4      pK:  	   R# V^ 8X  d   W,          '       d    R# V^8X  g   K  \        V 4      '       g   K   R# 	  \        R4      h)rJ   rw   TFzDappropriate value for D cannot be found in is_strong_selfridge_prp()r   )r`   rM   r   ro   rB   r@   r   )r	   rk   rL   r0   rl   rm   rn   ra   s   &       r   _is_strong_selfridge_prpr   7  s    1i#q55A1L7!a% A&q!acaZ!a%AFHA"Ava1q5\S1R4Z1$6Q]	 "
 6aee7y||' $( [
\\r   c                 t    V ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        V 4      # )r   z8is_strong_selfridge_prp() requires 'n' be greater than 0F)r   r   r4   s   &r   is_strong_selfridge_prpr   O  s>    1uSTTAv1uzAv#A&&r   c                     V ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        V ^4      ;'       d    \        V 4      # )r   z,is_bpsw_prp() requires 'n' be greater than 0F)r   rb   rx   r4   s   &r   is_bpsw_prpr   Y  sN    1uGHHAv1uzAv!Q88$5a$88r   c                     V ^8  d   \        R4      hV ^8X  d   R# V ^,          ^ 8X  d   V ^8H  # \        V ^4      ;'       d    \        V 4      # )r   z3is_strong_bpsw_prp() requires 'n' be greater than 0F)r   rb   r   r4   s   &r   is_strong_bpsw_prpr   c  sN    1uNOOAv1uzAv!Q??$<Q$??r   )r   )%rS   mpmath.libmplibmpr&   r   r`   rL   r   r   r#   r)   r,   r/   rK   lcmr5   r=   r@   rD   rF   rM   rP   rY   r\   r^   rb   rd   ro   rs   ru   rx   r{   r}   r   r   r   r   r3   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   