a
    kh@                     @   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 r| d@ s| dL } |d7 }qdn<|d? }| d@ s| d|> d @ r|dL }q| |L } ||7 }q|t| d@   S )N   r   r   i,  )abs_small_trailing
bit_length)xnZlow_bytetzp r   D/var/www/auris/lib/python3.9/site-packages/sympy/external/ntheory.py	bit_scan1   s,    

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r8t| }| |? |fS d}t| |\}}|s|} |d7 }|dkr|d g}|r|d }t| |\}}|s|dt|> 7 }|} ||d  ql|  qlt| |\}}qJ| |fS )N   zfactor must be > 1r   )r   r   r      )
ValueErrorr   divmodlenappendpop)r
   fbmyremZpow_list_fr   r   r   remove4   s0    

r!   c                 C   s   t tt | S )z
Return x!.)intmlibZifacr
   r   r   r   	factorialP   s    r%   c                 C   s   t tt | S )zInteger square root of x.)r"   r#   isqrtr$   r   r   r   sqrtU   s    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    
r-   c                 C   s   | r|s2t | pt |}|s dS || | || fS t| \}} t|\}}d\}}d\}}|rt| |\}	}
||
 } }|||	|   }}|||	|   }}qZ| || || fS )N)r   r   r   )r   r   r   r   )r   r-   r   )ar   gZx_signZy_signr
   r+   r   r*   qcr   r   r   gcdextj   s    
r3   c                 C   sz   | dk rdS dd| d@ > @ r dS | d }dd|d > @ r<dS d	d|d
 > @ rPdS dd|d > @ rdd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    r9   c                 C   s.   zt | d|W S  ty(   tdY n0 dS )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 existsN)powr   ZeroDivisionErrorr8   r   r   r   invert   s    r<   c                 C   sH   |dks|d st d| |; } | s(dS t| |d d |dkrDd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   r:   )r
   r   r   r   r   legendre   s    r=   c                 C   s   |dks|d st d| |; } | s0t|dkS |dks@| dkrDdS t| |dkrVdS d}| dkr| d dkr| dkr| dL } |d dv rb| }qb||  } }| d |d   krdkrn n| }| |; } qZ|S )	zJacobi symbol (x / y).r   r   z#y should be an odd positive integerr   r      r      r?   )r   r"   gcd)r
   r   jr   r   r   jacobi   s(    
 
rC   c                 C   sv   t | |dkrdS |dkrdS |dk r2| dk r2dnd}t|}t|}||L }|d rh| d dv rh| }|t| | S )zKronecker symbol (x / y).r   r   r   r   r   r>   )rA   r   r   rC   )r
   r   signr*   r   r   r   	kronecker   s    rE   c           	      C   s  | dk rt d|dk r t d| dv r0| dfS |dkr@| dfS |dkrdt| \}}t|| fS ||  krtdS zt| d	|  d
 }W nZ ty   t| | }|dkrt|d }td||  d |> }ntd| }Y n0 |dkr>d| }}||d  }||d | | |  |  }}t|| dk rqBqn|}|| }|| k rh|d7 }|| }qJ|| kr|d8 }|| }qh||| kfS )Nr   zy must be nonnegativer   zn must be positiver.   Tr   )r   Fg      ?g      ?5   g       @l           r   )	r   r#   r)   r"   r	   OverflowErrormathlog2r   )	r   r   r
   r   guessexpshiftZxprevr   r   r   r   iroot   sH    



rM   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   rA   r:   r   r/   r   r   r   is_fermat_prp  s    rO   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   rA   r:   rC   rN   r   r   r   is_euler_prp%  s    rP   c                 C   sv   t | d }t|| |? | }|dks0|| d kr4dS t|d D ]0}t|d| }|| d krb dS |dkr@ dS q@dS )Nr   Tr   F)r   r:   range)r   r/   r*   _r   r   r   _is_strong_prp4  s    rS   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   rA   rS   rN   r   r   r   is_strong_prpB  s    rT   c           	      C   s  |dkrdS |d d|  }d}|}||  }|dkrt |dd D ]x}|| |  }|| d |  }|dkrD|| | || ||   }}|d@ r|| 7 }|d@ r|| 7 }|d? |d?  }}qDn|dkrt|d	krtt |dd D ]}|| |  }|dkr|| d |  }n|| d |  }d}|dkr|| |d>  }}|d@ rR|| 7 }|dL }||7 }d	}q|| ; }nL|dkrt |dd D ]}|| |  }|| d|  |  }||9 }|dkr|| || d>  }}|d@ r|| 7 }|dL }|| }||9 }|| ; }qnt |dd D ]}|| |  }|| d|  |  }||9 }|dkr|| | || ||   }}|d@ r|| 7 }|d@ r|| 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   r@   r   r?   N1r   )bin)	r   PQkDUVQkr   r   r   r   _lucas_sequenceQ  sv    9







r^   c                 C   sz   |d d|  }|dks(|dks(|dvr0t d| dk r@t d| dkrLdS | d dkr`| dkS t| ||| d ||  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   r1   dr   r   r   is_fibonacci_prp  s    ra   c                 C   s   |d d|  }|dkr t d| dk r0t d| dkr<dS | d dkrP| dkS t| || d| fvrnt dt| ||| t||  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   rA   r^   rC   r_   r   r   r   is_lucas_prp  s    rb   c                 C   s   t dddD ]v}|d@ r| }t|| }|dkrVt| dd| d | d d dk  S |dkrl||  rl dS |d	krt| r dS qtd
dS )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()N)rQ   rC   r^   r9   r   )r   rZ   rB   r   r   r   _is_selfridge_prp  s    
&re   c                 C   s8   | dk rt d| dkrdS | d dkr0| dkS t| S )Nr   1is_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   re   r,   r   r   r   is_selfridge_prp  s    rg   c           
      C   s   |d d|  }|dkr t d| dk r0t d| dkr<dS | d dkrP| dkS t| || d| fvrnt dt|| }t| | }t| ||| | |? \}}}|dks|dkrd	S t|d D ]2}	|| d|  |  }|dkr d	S t|d| }qdS )
Nr   r@   r   z/invalid values for p,q in is_strong_lucas_prp()r   rf   Fz0is_strong_lucas_prp() requires gcd(n,2*q*D) == 1T)r   rA   rC   r   r^   rQ   r:   )
r   r   r1   rZ   rB   r*   r[   r\   r]   rR   r   r   r   is_strong_lucas_prp  s,    
rh   c                 C   s   t dddD ]}|d@ r| }t|| }|dkrt| d }t| dd| d | d |? \}}}|dksp|dkrv dS t |d D ]4}|| d|  |  }|dkr  dS t|d| }q d	S |dkr||  r d	S |d
krt| r d	S qtdd S )Nr   rc   r   r   r   r@   r   TFrd   zDappropriate value for D cannot be found in is_strong_selfridge_prp())rQ   rC   r   r^   r:   r9   r   )r   rZ   rB   r*   r[   r\   r]   rR   r   r   r   _is_strong_selfridge_prp7  s(    
$ri   c                 C   s8   | dk rt d| dkrdS | d dkr0| dkS t| S )Nr   z8is_strong_selfridge_prp() requires 'n' be greater than 0Fr   r   )r   ri   r,   r   r   r   is_strong_selfridge_prpO  s    rj   c                 C   sB   | dk rt d| dkrdS | d dkr0| dkS t| do@t| S )Nr   z,is_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   rS   re   r,   r   r   r   is_bpsw_prpY  s    rk   c                 C   sB   | dk rt d| dkrdS | d dkr0| dkS t| do@t| S )Nr   z3is_strong_bpsw_prp() requires 'n' be greater than 0Fr   r   )r   rS   ri   r,   r   r   r   is_strong_bpsw_prpc  s    rl   )r   )r   )%rH   Zmpmath.libmpZlibmpr#   r   rQ   rB   r   r   r!   r%   r'   r)   rA   lcmr-   r3   r9   r<   r=   rC   rE   rM   rO   rP   rS   rT   r^   ra   rb   re   rg   rh   ri   rj   rk   rl   r   r   r   r   <module>   sD   
*
 
$.(


