o
    3Ih@                     @   sJ  d dl Z d dlmZ d gd ZeddD ]Zegdde >  ede> dded > < qd>ddZd>dd	Zd
d Z	dd Z
dd Zdd Ze jZe j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&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"d:d; Z#d<d= Z$dS )?    N            c                 C   s   | sd S t | |? } | d@ }|rt| | S d| }| dL } |  d }| d|> kr.|| S |dk rC| d@ sB| dL } |d7 }| d@ r6n(|d? }| d@ sk| d|> d @ r_|dL }| d|> d @ sS| |L } ||7 }| d@ rK|t| d@   S )N   r   r   i,  )abs_small_trailing
bit_length)xnlow_bytetzp r   V/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sympy/external/ntheory.py	bit_scan1   s4   r   c                 C   s   t | d|>  |S )Nr   )r   )r
   r   r   r   r   	bit_scan00   s   r   c                 C   s   |dk rt d| dkrdS |dkrt| }| |? |fS d}t| |\}}|sf|} |d7 }|dkr]|d g}|r]|d }t| |\}}|sW|dt|> 7 }|} ||d  n|  |s8t| |\}}|r'| |fS )N   zfactor must be > 1r   )r   r   r      )
ValueErrorr   divmodlenappendpop)r
   fbmyrempow_list_fr   r   r   remove4   s4   
	r#   c                 C      t tt | S )z
Return x!.)intmlibifacr
   r   r   r   	factorialP      r)   c                 C   r$   )zInteger square root of x.)r%   r&   isqrtr(   r   r   r   sqrtU   r*   r,   c                 C   s"   t t| \}}t|t|fS )z'Integer square root of x and remainder.r&   sqrtremr%   )r
   srr   r   r   r.   Z   s   r.   c                 C   s   | dk r	d|  fS d| fS )Nr   r   r   r   r   r   r   r   _signd   s   
r2   c                 C   s   | r|st | pt |}|sdS || | || fS t| \}} t|\}}d\}}d\}}|rOt| |\}	}
||
} }|||	|  }}|||	|  }}|s/| || || fS )N)r   r   r   )r   r   r   r   )r   r2   r   )ar   gx_signy_signr
   r0   r   r/   qcr   r   r   gcdextj   s    
r:   c                 C   sz   | dk rdS dd| d@ > @ rdS | d }dd|d > @ rdS d	d|d
 > @ r(dS dd|d > @ r2dS t 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   r-   r
   r   r   r   r   	is_square   s   r@   c                 C   s&   zt | d|W S  ty   td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   ZeroDivisionErrorr?   r   r   r   invert   s
   rC   c                 C   sH   |dks|d st d| |; } | sdS t| |d d |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   rA   )r
   r   r   r   r   legendre   s   rD   c                 C   s   |dks|d st d| |; } | st|dkS |dks | dkr"dS t| |dkr+dS d}| dkrr| d dkrR| dkrR| dL } |d dv rH| }| d dkrR| dks;|| } }| d |d   kredkrjn n| }| |; } | dks1|S )	zJacobi symbol (x / y).r   r   z#y should be an odd positive integerr   r      r      rF   )r   r%   gcd)r
   r   jr   r   r   jacobi   s,   
 	rJ   c                 C   sv   t | |dkr	dS |dkrdS |dk r| dk rdnd}t|}t|}||L }|d r4| d dv r4| }|t| | S )zKronecker symbol (x / y).r   r   r   r   r   rE   )rH   r   r   rJ   )r
   r   signr/   r   r   r   	kronecker   s   rL   c           	      C   s  | dk rt d|dk rt d| dv r| dfS |dkr | dfS |dkr2t| \}}t|| fS ||  kr:dS zt| d	|  d
 }W n- tys   t| | }|dkrkt|d }td||  d |> }ntd| }Y nw |dkrd|}}	 ||d  }||d | | |  | }}t|| dk rnq~n|}|| }|| k r|d7 }|| }|| k s|| kr|d8 }|| }|| ks||| kfS )Nr   zy must be nonnegativer   zn must be positiver3   Tr   )r   Fg      ?g      ?5   g       @l           r   )	r   r&   r.   r%   r	   OverflowErrormathlog2r   )	r   r   r
   r    guessexpshiftxprevr   r   r   r   iroot   sV   
rU   c                 C   sr   |dk rt d| dk rt d| dkrdS | d dkr | dkS || ; }t| |dkr/t dt|| d | 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   rH   rA   r   r4   r   r   r   is_fermat_prp  s   rW   c                 C   s|   |dk rt d| dk rt d| dkrdS | d dkr | dkS || ; }t| |dkr/t dt|| d? | t|| |  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   rH   rA   rJ   rV   r   r   r   is_euler_prp%  s   rX   c                 C   sv   t | d }t|| |? | }|dks|| d krdS t|d D ]}t|d| }|| d kr1 dS |dkr8 dS q dS )Nr   Tr   F)r   rA   range)r   r4   r/   _r   r   r   _is_strong_prp4  s   r[   c                 C   sh   |dk rt d| dk rt d| dkrdS | d dkr | dkS || ; }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   rH   r[   rV   r   r   r   is_strong_prpB  s   
r\   c           	      C   s  |dkrdS |d d|  }d}|}||  }|dkr`t |dd D ]<}|| |  }|| d |  }|dkr^|| | || ||  }}|d@ rM|| 7 }|d@ rU|| 7 }|d? |d? }}q"n|dkr|d	krt |dd D ]>}|| |  }|dkr|| d |  }n
|| d |  }d}|dkr|| |d> }}|d@ r|| 7 }|dL }||7 }d	}qp|| ; }n|dkrt |dd D ]=}|| |  }|| d|  |  }||9 }|dkr|| || d> }}|d@ r|| 7 }|dL }|| }||9 }|| ; }qnWt |dd D ]N}|| |  }|| d|  |  }||9 }|dkrP|| | || ||  }}|d@ r:|| 7 }|d@ rC|| 7 }|d? |d? }}||9 }|| ; }q||  ||  |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   rG   r   rF   N1r   )bin)	r   PQkDUVQkr   r   r   r   _lucas_sequenceQ  s~   9





rf   c                 C   sz   |d d|  }|dks|dks|dvrt d| dk r t d| dkr&dS | d dkr0| dkS t| ||| d ||  kS )	Nr   rG   r   )r   r   z,invalid values for p,q in is_fibonacci_prp()r   z1is_fibonacci_prp() requires 'n' be greater than 0F)r   rf   r   r   r8   dr   r   r   is_fibonacci_prp  s   ri   c                 C   s   |d d|  }|dkrt d| dk rt d| dkrdS | d dkr(| dkS t| || d| fvr7t dt| ||| t||  d dkS )	Nr   rG   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   rH   rf   rJ   rg   r   r   r   is_lucas_prp  s    rj   c                 C   s   t dddD ];}|d@ r| }t|| }|dkr+t| dd| d | d d dk  S |dkr6||  r6 dS |d	krAt| rA dS qt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   rG   r   F   z=appropriate value for D cannot be found in is_selfridge_prp())rY   rJ   rf   r@   r   )r   rb   rI   r   r   r   _is_selfridge_prp  s   
&rm   c                 C   8   | dk rt d| dkrdS | d dkr| dkS t| S )Nr   1is_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   rm   r1   r   r   r   is_selfridge_prp     rp   c           
      C   s   |d d|  }|dkrt d| dk rt d| dkrdS | d dkr(| dkS t| || d| fvr7t dt|| }t| | }t| ||| | |? \}}}|dksX|dkrZd	S t|d D ]}	|| d|  |  }|dkrs d	S t|d| }q`dS )
Nr   rG   r   z/invalid values for p,q in is_strong_lucas_prp()r   ro   Fz0is_strong_lucas_prp() requires gcd(n,2*q*D) == 1T)r   rH   rJ   r   rf   rY   rA   )
r   r   r8   rb   rI   r/   rc   rd   re   rZ   r   r   r   is_strong_lucas_prp  s,   
rr   c                 C   s   t dddD ]o}|d@ r| }t|| }|dkr_t| d }t| dd| d | d |? \}}}|dks8|dkr; dS t |d D ]}|| d|  |  }|dkrU  dS t|d| }qA d	S |dkrj||  rj d	S |d
krut| ru d	S qtd)Nr   rk   r   r   r   rG   r   TFrl   zDappropriate value for D cannot be found in is_strong_selfridge_prp())rY   rJ   r   rf   rA   r@   r   )r   rb   rI   r/   rc   rd   re   rZ   r   r   r   _is_strong_selfridge_prp7  s*   
$rs   c                 C   rn   )Nr   z8is_strong_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   rs   r1   r   r   r   is_strong_selfridge_prpO  rq   rt   c                 C   B   | dk rt d| dkrdS | d dkr| dkS t| do t| S )Nr   z,is_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   r[   rm   r1   r   r   r   is_bpsw_prpY     rv   c                 C   ru   )Nr   z3is_strong_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   r[   rs   r1   r   r   r   is_strong_bpsw_prpc  rw   rx   )r   )%rO   mpmath.libmplibmpr&   r   rY   rI   r   r   r#   r)   r,   r.   rH   lcmr2   r:   r@   rC   rD   rJ   rL   rU   rW   rX   r[   r\   rf   ri   rj   rm   rp   rr   rs   rt   rv   rx   r   r   r   r   <module>   sF   
*

 $.(


