ó
¡¼™\c           @  sÜ  d  d l  m Z m Z d  d l m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z d  d l m Z d d l m Z d d	 l m Z m Z m Z m Z d  d
 l m Z m Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z  d „  Z! e" d „ Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ e d „ Z, d „  Z- d „  Z. d „  Z/ d e f d  „  ƒ  YZ0 d' d! „ Z2 d' d" „ Z3 d' d# d' d$ „ Z4 d' d% „ Z5 d' d' d& „ Z6 d' S((   iÿÿÿÿ(   t   print_functiont   division(   t   as_intt   range(   t   Function(   t   igcdt   igcdext   mod_inverse(   t   isqrt(   t   Si   (   t   isprime(   t	   factorintt   trailingt   totientt   multiplicity(   t   randintt   Randomc         C  s°  d d l  m } t |  ƒ t | ƒ }  } t |  | ƒ d k rM t d ƒ ‚ n  | t ƒ } t | ƒ } xz | j ƒ  D]l \ } } | d k r¡ | | c | d 7<n  t | d ƒ } x* | j ƒ  D] \ } }	 | | c |	 7<q¾ Wqr Wd }
 x( | j ƒ  D] \ } } |
 | | 9}
 qõ Wd } |  | k r2|  | }  n  xw | j ƒ  D]i \ } } |
 } xT t | d ƒ D]B } t	 |  | | ƒ d k rš| | | | d 9} Pn  | | } qbWq?W| S(   s)  Returns the order of ``a`` modulo ``n``.

    The order of ``a`` modulo ``n`` is the smallest integer
    ``k`` such that ``a**k`` leaves a remainder of 1 with ``n``.

    Examples
    ========

    >>> from sympy.ntheory import n_order
    >>> n_order(3, 7)
    6
    >>> n_order(4, 7)
    3
    iÿÿÿÿ(   t   defaultdicti   s*   The two numbers should be relatively prime(
   t   collectionsR   R   R   t
   ValueErrort   intR   t   itemsR   t   pow(   t   at   nR   t   factorst   ft   pxt   kxt   fpxt   pyt   kyt   group_ordert   ordert   pt   et   exponent(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   n_order   s4    c         c  sŠ   g  t  |  d ƒ j ƒ  D] } |  d | ^ q } d } xL | |  k  r… x/ | D]" } t | | |  ƒ d k rM PqM qM W| V| d 7} q: Wd S(   sJ  
    Generates the primitive roots for a prime ``p``

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import _primitive_root_prime_iter
    >>> list(_primitive_root_prime_iter(19))
    [2, 3, 10, 13, 14, 15]

    References
    ==========

    .. [1] W. Stein "Elementary Number Theory" (2011), page 44

    i   i   N(   R   t   keysR   (   R"   t   it   vR   t   pw(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _primitive_root_prime_iter;   s    1c         C  sÏ  t  |  ƒ }  |  d k  r' t d ƒ ‚ n  |  d k r7 d St |  ƒ } t | ƒ d k rY d St | ƒ d k rd | k s‡ | d d k r‹ d Sx* | j ƒ  D] \ } } | d k r˜ Pq˜ q˜ Wd } xþ | |  k  r| d 7} | | d k rí qÁ n  t | |  ƒ rÁ | SqÁ Wn¸ d | k r'|  d k r#d Sd St | j ƒ  ƒ d \ } } | d k r¿t | ƒ } t | | d ƒ rr| SxJ t	 d | | d ƒ D]. } t
 | |  ƒ d k rŠt | |  ƒ rŠ| SqŠWn  t t |  ƒ ƒ S(   s  
    Returns the smallest primitive root or None

    Parameters
    ==========

    p : positive integer

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import primitive_root
    >>> primitive_root(19)
    2

    References
    ==========

    .. [1] W. Stein "Elementary Number Theory" (2011), page 44
    .. [2] P. Hackman "Elementary Number Theory" (2009), Chapter C

    i   s   p is required to be positivei   i    i   i   N(   R   R   R   t   lent   NoneR   t   is_primitive_roott   listt   primitive_rootR   R   t   nextR*   (   R"   R   t   p1t   e1R'   R   t   g(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR/   Y   sD    
$c         C  so   t  |  ƒ t  | ƒ }  } t |  | ƒ d k r= t d ƒ ‚ n  |  | k rV |  | }  n  t |  | ƒ t | ƒ k S(   s÷  
    Returns True if ``a`` is a primitive root of ``p``

    ``a`` is said to be the primitive root of ``p`` if gcd(a, p) == 1 and
    totient(p) is the smallest positive number s.t.

        a**totient(p) cong 1 mod(p)

    Examples
    ========

    >>> from sympy.ntheory import is_primitive_root, n_order, totient
    >>> is_primitive_root(3, 10)
    True
    >>> is_primitive_root(9, 10)
    False
    >>> n_order(3, 10) == totient(10)
    True
    >>> n_order(9, 10) == totient(10)
    False

    i   s*   The two numbers should be relatively prime(   R   R   R   R%   R   (   R   R"   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR-   ›   s    c         C  s'  t  | d ƒ } | | ?} x6 t d | d ƒ } t | | ƒ } | d k r Pq q Wt |  | | ƒ } t | | | ƒ } d } xq t | ƒ D]c }	 | t | | | ƒ | }
 t |
 d | d |	 | ƒ }
 |
 | | d k rŠ | d |	 7} qŠ qŠ Wt |  | d d | ƒ t | | d | ƒ | } | S(   sÀ   
    Returns the square root in the case of ``p`` prime with ``p == 1 (mod 8)``

    References
    ==========

    .. [1] R. Crandall and C. Pomerance "Prime Numbers", 2nt Ed., page 101

    i   i   iÿÿÿÿi    (   R   R   t   legendre_symbolR   R   (   R   R"   t   st   tt   dt   rt   At   Dt   mR'   t   admt   x(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _sqrt_mod_tonelli_shanksº   s"    

2c         C  sÕ   | r t  t t |  | ƒ ƒ ƒ Sy t t | ƒ ƒ } t |  | ƒ } t | ƒ } | | d k rg | | S| | d k  r{ | Sy( t | ƒ } | | d k r¢ | | SWn t k
 r¶ n X| SWn t k
 rÐ d SXd S(   sŸ  
    Find a root of ``x**2 = a mod p``

    Parameters
    ==========

    a : integer
    p : positive integer
    all_roots : if True the list of roots is returned or None

    Notes
    =====

    If there is no root it is returned None; else the returned root
    is less or equal to ``p // 2``; in general is not the smallest one.
    It is returned ``p // 2`` only if it is the only root.

    Use ``all_roots`` only when it is expected that all the roots fit
    in memory; otherwise use ``sqrt_mod_iter``.

    Examples
    ========

    >>> from sympy.ntheory import sqrt_mod
    >>> sqrt_mod(11, 43)
    21
    >>> sqrt_mod(17, 32, True)
    [7, 9, 23, 25]
    i   N(   t   sortedR.   t   sqrt_mod_itert   absR   R0   t   StopIterationR,   (   R   R"   t	   all_rootst   itR8   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   sqrt_modÚ   s&    c          '  sÃ   d d l  ‰  t ‡  f d †  |  Dƒ ƒ } t | ƒ } d g | } t } xu t r¾ d | } } x5 | r” | r” | d 8} t | | ƒ \ } | | <q` W| r¶ | r¶ | r² t } q¶ Pn  | VqJ Wd S(   sð   
    Cartesian product generator

    Notes
    =====

    Unlike itertools.product, it works also with iterables which do not fit
    in memory. See http://bugs.python.org/issue10109

    Author: Fernando Sumudu
    with small changes
    iÿÿÿÿNc         3  s$   |  ] } ˆ  j  t | ƒ ƒ Vq d  S(   N(   t   cyclet	   enumerate(   t   .0RD   (   t	   itertools(    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pys	   <genexpr>  s    i    i   (   RI   t   tupleR+   R,   t   TrueR0   t   False(   t   iterst	   inf_iterst	   num_iterst   cur_valt   first_vR'   R"   (    (   RI   s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _product  s    	
	c         c  s  d d l  m } m } d d l m } t |  ƒ t t | ƒ ƒ }  } t | ƒ rÛ |  | }  |  d k r| t |  | d ƒ } n t	 |  | d ƒ } | r| | k r¹ x2 | D] } | Vq§ WqØ x | D] } | | ƒ VqÀ Wqn9t
 | ƒ } g  }	 g  }
 xƒ | j ƒ  D]u \ } } |  | d k r;t |  | | ƒ } | sWd Sn t	 |  | | ƒ } | sWd S|	 j | ƒ |
 j | | ƒ q W| |
 | ƒ \ } } } | | k r×xt t |	 Œ  D]& } | | |
 | | | | ƒ } | VqªWn= x: t |	 Œ  D], } | | |
 | | | | ƒ } | | ƒ VqäWd S(   sH  
    Iterate over solutions to ``x**2 = a mod p``

    Parameters
    ==========

    a : integer
    p : positive integer
    domain : integer domain, ``int``, ``ZZ`` or ``Integer``

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import sqrt_mod_iter
    >>> list(sqrt_mod_iter(11, 43))
    [21, 22]
    iÿÿÿÿ(   t   gf_crt1t   gf_crt2(   t   ZZi    i   N(   t   sympy.polys.galoistoolsRS   RT   t   sympy.polys.domainsRU   R   RA   R
   t
   _sqrt_mod1t   _sqrt_mod_prime_powerR   R   t   appendRR   (   R   R"   t   domainRS   RT   RU   t   resR=   R   R(   t   pvR   t   ext   rxt   mmR#   R5   t   vxR8   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR@   0  sF    
c         C  s  d d l  m } d d l m } | | } |  | }  | d k ro| d k rY | |  ƒ g St |  | ƒ sl d S| d d k r™ t |  | d d | ƒ } n¶ | d d	 k r@t |  | d d | ƒ } | d k rì t |  | d d | ƒ } qOt d |  | d	 d | ƒ } d |  | | }	 t |	 d | ƒ |  k rO|	 } qOn t |  | ƒ } t | | ƒ | | | ƒ g ƒ S| d k r| d k r|  d d k r›d S| d k rùt	 ƒ  }
 x< t
 d
 | d ƒ D]( } |
 j d | ƒ |
 j d | ƒ qÃWt |
 ƒ S| d ƒ | d ƒ | d	 ƒ | d ƒ g } d } g  } x× | D]Ï } | } xK | | k  r| d |  | ?} | d r‚| d | d >} n  | d 7} qEW| | k r¬| j | ƒ n  | d | d >}	 |	 d | >k  r6|	 | k r6|	 d |  | d
 k r| j |	 ƒ qq6q6W| St |  | d ƒ } | s)d S| d
 } | d |  } d } | } xk | } | d 9} | | k rpPn  | } | d } | d | | ƒ d
 } | | | | } | d |  } qPW| | k  rý| | } | d | | ƒ d
 } | | | | } n  | | | g Sd S(   sõ  
    Find the solutions to ``x**2 = a mod p**k`` when ``a % p != 0``

    Parameters
    ==========

    a : integer
    p : prime number
    k : positive integer

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import _sqrt_mod_prime_power
    >>> _sqrt_mod_prime_power(11, 43, 1)
    [21, 22]

    References
    ==========

    .. [1] P. Hackman "Elementary Number Theory" (2009), page 160
    .. [2] http://www.numbertheory.org/php/squareroot.html
    .. [3] [Gathen99]_
    iÿÿÿÿ(   R   (   RU   i   i   i   i   i   i   i    i   N(   t   sympy.core.numbersR   RW   RU   t   is_quad_residueR,   R   R>   R?   t   setR   t   addR.   RZ   RY   (   R   R"   t   kR   RU   t   pkR\   t   signt   bR=   R5   R'   t   rvR   R8   t   nxt   r1t   frR   t   n1t   frinv(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyRY   l  sŒ    

 	
*




c           sz  ˆ | ‰ |  ˆ }  |  d k r‡ | d ‰  | d d k ra ˆ ˆ  d ‰ ‡ ‡ f d †  } | ƒ  Sˆ ˆ  ‰ ‡ ‡ f d †  } | ƒ  Sn  t  |  ƒ } | ˆ } | d d k r± d
 S| d ‰  |  | ?} ˆ d k rí| | d k r d | ˆ  d >‰ d ˆ  d >‰ ‡  ‡ ‡ ‡ f d †  } | ƒ  S| | d k rƒt | ˆ | | ƒ ‰ ˆ d
 k rVd
 Sd | ˆ  >‰ ‡  ‡ ‡ ‡ f d †  }	 |	 ƒ  S| | d k rvt | ˆ | | ƒ ‰ ˆ d
 k r¹d
 Sd | ˆ  d >‰ ‡  ‡ ‡ ‡ f d †  }
 |
 ƒ  Sn‰ | d ‰  |  ˆ | } t | ˆ | | ƒ ‰	 ˆ	 d
 k r+d
 Sˆ ˆ  ‰ ˆ | | ‰ ˆ | ˆ  ‰ ‡  ‡ ‡ ‡ ‡ ‡	 f d	 †  } | ƒ  Sd
 S(   s~   
    Find solution to ``x**2 == a mod p**n`` when ``a % p == 0``

    see http://www.numbertheory.org/php/squareroot.html
    i    i   i   c          3  s,   d }  x |  ˆ k  r' |  V|  ˆ  7}  q	 Wd  S(   Ni    (    (   R'   (   t   pm1t   pn(    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _iter0aî  s    c          3  s,   d }  x |  ˆ k  r' |  V|  ˆ  7}  q	 Wd  S(   Ni    (    (   R'   (   t   pmRq   (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _iter0bö  s    c          3  sa   d ˆ  d >}  d ˆ  >} xB | ˆ k  r\ | } x | ˆ k  rN | V| |  7} q0 W| ˆ 7} q Wd  S(   Ni   i   (    (   Rf   R'   t   j(   R;   Rp   Rq   t   pnm1(    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _iter1  s    
c          3  sp   t  ƒ  }  x` ˆ D]X } d } xI | ˆ k  rg | ˆ  >| } | |  k rZ |  j | ƒ | Vn  | ˆ 7} q Wq Wd  S(   Ni    (   Rd   Re   (   R5   R8   R'   R=   (   R;   Rq   t   pnmR\   (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _iter2  s    	c          3  st   t  ƒ  }  xd ˆ D]\ } d } xM | ˆ k  rk | ˆ  >| ˆ } | |  k r^ |  j | ƒ | Vn  | ˆ 7} q Wq Wd  S(   Ni    (   Rd   Re   (   R5   R8   R'   R=   (   R;   Rq   Rv   R\   (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _iter3'  s    	c          3  s~   t  ƒ  }  ˆ ˆ  } xd ˆ D]\ } d } xM | ˆ k  ru | | ˆ } | |  k rh |  j | ƒ | | Vn  | ˆ 7} q) Wq Wd  S(   Ni    (   Rd   Re   (   R5   Rs   R_   R'   R=   (   R;   R"   Rq   Rx   t   pnrt   res1(    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _iter4<  s    	
N(   R   R,   RY   (   R   R"   R   Rr   Rt   R   R8   t   a1Rw   Ry   Rz   R}   (    (
   R;   R"   Rs   Rp   Rq   Rx   Rv   R{   R\   R|   s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyRX   á  s^    







	




c         C  sè   t  |  ƒ t  | ƒ }  } | d k  r4 t d ƒ ‚ n  |  | k sL |  d k  rY |  | }  n  |  d k  sq | d k  ru t St | ƒ sÊ | d r¤ t |  | ƒ d k r¤ t St |  | ƒ } | d k rÃ t St Sn  t |  | d d | ƒ d k S(   sÕ  
    Returns True if ``a`` (mod ``p``) is in the set of squares mod ``p``,
    i.e a % p in set([i**2 % p for i in range(p)]). If ``p`` is an odd
    prime, an iterative method is used to make the determination:

    >>> from sympy.ntheory import is_quad_residue
    >>> sorted(set([i**2 % 7 for i in range(7)]))
    [0, 1, 2, 4]
    >>> [j for j in range(7) if is_quad_residue(j, 7)]
    [0, 1, 2, 4]

    See Also
    ========

    legendre_symbol, jacobi_symbol
    i   s   p must be > 0i    i   i   iÿÿÿÿN(	   R   R   RK   R
   t   jacobi_symbolRL   RE   R,   R   (   R   R"   R8   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyRc   J  s    c         C  sÖ   t  |  ƒ t  | ƒ t  | ƒ }  } } | d k rA t d ƒ ‚ n  | d k  r\ t d ƒ ‚ n  |  d k  rw t d ƒ ‚ n  | d k r | d k r“ t S|  d k S| d k r­ t S| d k rÆ t |  | ƒ St |  | | ƒ S(   s    
    Returns True if ``x**n == a (mod m)`` has solutions.

    References
    ==========

    .. [1] P. Hackman "Elementary Number Theory" (2009), page 76

    i    s   m must be > 0s   n must be >= 0s   a must be >= 0i   i   (   R   R   RL   RK   Rc   t   _is_nthpow_residue_bign(   R   R   R;   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   is_nthpow_residuen  s     
&
c         C  s‡   t  | ƒ d k rR x9 t | ƒ j ƒ  D]% \ } } t |  | | | ƒ s% t Sq% Wt St | ƒ } | t | | ƒ } t	 |  | | ƒ d k S(   s>   Returns True if ``x**n == a (mod m)`` has solutions for n > 2.i   N(
   R/   R,   R   R   t#   _is_nthpow_residue_bign_prime_powerRL   RK   R   R   R   (   R   R   R;   t   primet   powerR   Rf   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR€   Š  s    c         C  sÕ   |  | rm | d k r/ t  |  | t | | ƒ ƒ S| d @r= t St | ƒ } |  t d t | d | ƒ ƒ d k S|  t | | ƒ ;}  |  sŠ t St | |  ƒ } | | r§ t St | | ƒ } t |  | | | | | ƒ Sd S(   sV   Returns True/False if a solution for ``x**n == a (mod(p**k))``
    does/doesn't exist.i   i   N(   R€   R   RK   R   t   minR   RL   R‚   (   R   R   R"   Rf   t   ct   muRs   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR‚   ™  s    

$
c         C  sm   t  | ƒ } g  } x. | j ƒ  D]  \ } } | j | g | ƒ q Wx# | D] } t |  | | t ƒ }  qJ W|  S(   N(   R   R   t   extendt   _nthroot_mod1RL   (   R5   t   qR"   R   R(   Ri   R#   t   qx(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _nthroot_mod2²  s    c         C  s²  t  | ƒ } t | ƒ s- t |  | | ƒ } n| d } | d | d k sQ t ‚ d } x( | | d k r | d 7} | | } qZ Wt | | ƒ d | } | | }	 d |	 | }
 t |  |
 | ƒ } t |  | | ƒ } t | | | | ƒ } t | | | ƒ } t | |	 | | ƒ } t | | ƒ d } | | | } | g } t | | d | | ƒ } | } x3 t | d ƒ D]! } | | | } | j | ƒ qoW| r¨| j	 ƒ  | St
 | ƒ S(   s«   
    Root of ``x**q = s mod p``, ``p`` prime and ``q`` divides ``p - 1``

    References
    ==========

    .. [1] A. M. Johnston "A Generalized qth Root Algorithm"

    i   i    (   R/   R
   RŒ   t   AssertionErrorR   R   t   discrete_logR   RZ   t   sortR…   (   R5   RŠ   R"   RC   R3   R8   R   Rf   t   f1t   zR=   Rl   t   s1t   hR6   t   g2t   g3R\   t   hxR'   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR‰   ¼  s:    



	
c         C  s¾  d d l  m } t |  ƒ t | ƒ t | ƒ }  } } | d k rR t |  | | ƒ St |  | | ƒ sh d St | ƒ d k r‰ t d ƒ ‚ n  | d | d k r° t |  | | | ƒ S| } | d } d } | | k  ró | | |  | f \ }  } } } n  xl | rat	 | | ƒ \ } }	 t
 | | | ƒ }
 | |
 | ƒ d }
 |
 |  | }
 | |	 } } | |
 }  } qö W| d k r‰| r€|  g } qº|  } n1 | d k r¥t |  | | ƒ St |  | | | ƒ } | S(   s¶  
    Find the solutions to ``x**n = a mod p``

    Parameters
    ==========

    a : integer
    n : positive integer
    p : positive integer
    all_roots : if False returns the smallest root, else the list of roots

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import nthroot_mod
    >>> nthroot_mod(11, 4, 19)
    8
    >>> nthroot_mod(11, 4, 19, True)
    [8, 11]
    >>> nthroot_mod(68, 3, 109)
    23
    iÿÿÿÿ(   R   i   s,   Not Implemented for m without primitive rooti   i    N(   Rb   R   R   RE   R   R,   R/   t   NotImplementedErrorR‰   t   divmodR   (   R   R   R"   RC   R   t   pat   pbRi   RŠ   R8   R†   R\   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   nthroot_modê  s<    &
!		c         C  s]   t  |  ƒ }  t ƒ  } x5 t |  d d ƒ D] } | j t | d |  ƒ ƒ q* Wt t | ƒ ƒ S(   sÁ   
    Returns the list of quadratic residues.

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import quadratic_residues
    >>> quadratic_residues(7)
    [0, 1, 2, 4]
    i   i   (   R   Rd   R   Re   R   R?   R.   (   R"   R8   R'   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   quadratic_residues*  s
    	c         C  sl   t  |  ƒ t  | ƒ }  } t | ƒ s2 | d k rA t d ƒ ‚ n  |  | }  |  sU d St |  | ƒ rh d Sd S(   s  
    Returns the Legendre symbol `(a / p)`.

    For an integer ``a`` and an odd prime ``p``, the Legendre symbol is
    defined as

    .. math ::
        \genfrac(){}{}{a}{p} = \begin{cases}
             0 & \text{if } p \text{ divides } a\\
             1 & \text{if } a \text{ is a quadratic residue modulo } p\\
            -1 & \text{if } a \text{ is a quadratic nonresidue modulo } p
        \end{cases}

    Parameters
    ==========

    a : integer
    p : odd prime

    Examples
    ========

    >>> from sympy.ntheory import legendre_symbol
    >>> [legendre_symbol(i, 7) for i in range(7)]
    [0, 1, 1, -1, 1, -1, -1]
    >>> sorted(set([i**2 % 7 for i in range(7)]))
    [0, 1, 2, 4]

    See Also
    ========

    is_quad_residue, jacobi_symbol

    i   s   p should be an odd primei    i   iÿÿÿÿ(   R   R
   R   Rc   (   R   R"   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR4   <  s    #
c         C  s™  t  |  ƒ t  | ƒ }  } | d k  s0 | d r? t d ƒ ‚ n  |  d k  sW |  | k rd |  | }  n  |  sz t | d k ƒ S| d k s’ |  d k r– d St |  | ƒ d k r¯ d Sd } |  d k  rå |  }  | d d k rå | } qå n  x˜ |  d k rxD |  d d k r:|  d k r:|  d L}  | d d	 k r÷ | } q÷ q÷ W| |  }  } |  d d k rr| d d k rr| } n  |  | ;}  qè W| d k r•d } n  | S(
   sµ  
    Returns the Jacobi symbol `(m / n)`.

    For any integer ``m`` and any positive odd integer ``n`` the Jacobi symbol
    is defined as the product of the Legendre symbols corresponding to the
    prime factors of ``n``:

    .. math ::
        \genfrac(){}{}{m}{n} =
            \genfrac(){}{}{m}{p^{1}}^{\alpha_1}
            \genfrac(){}{}{m}{p^{2}}^{\alpha_2}
            ...
            \genfrac(){}{}{m}{p^{k}}^{\alpha_k}
            \text{ where } n =
                p_1^{\alpha_1}
                p_2^{\alpha_2}
                ...
                p_k^{\alpha_k}

    Like the Legendre symbol, if the Jacobi symbol `\genfrac(){}{}{m}{n} = -1`
    then ``m`` is a quadratic nonresidue modulo ``n``.

    But, unlike the Legendre symbol, if the Jacobi symbol
    `\genfrac(){}{}{m}{n} = 1` then ``m`` may or may not be a quadratic residue
    modulo ``n``.

    Parameters
    ==========

    m : integer
    n : odd positive integer

    Examples
    ========

    >>> from sympy.ntheory import jacobi_symbol, legendre_symbol
    >>> from sympy import Mul, S
    >>> jacobi_symbol(45, 77)
    -1
    >>> jacobi_symbol(60, 121)
    1

    The relationship between the ``jacobi_symbol`` and ``legendre_symbol`` can
    be demonstrated as follows:

    >>> L = legendre_symbol
    >>> S(45).factors()
    {3: 2, 5: 1}
    >>> jacobi_symbol(7, 45) == L(7, 3)**2 * L(7, 5)**1
    True

    See Also
    ========

    is_quad_residue, legendre_symbol
    i    i   s#   n should be an odd positive integeri   i   i   i   i   (   i   i   (   R   R   R   R   (   R;   R   Ru   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR   j  s8    9
 
	t   mobiusc           B  s   e  Z d  Z e d „  ƒ Z RS(   s¥  
    MÃ¶bius function maps natural number to {-1, 0, 1}

    It is defined as follows:
        1) `1` if `n = 1`.
        2) `0` if `n` has a squared prime factor.
        3) `(-1)^k` if `n` is a square-free positive integer with `k`
           number of prime factors.

    It is an important multiplicative function in number theory
    and combinatorics.  It has applications in mathematical series,
    algebraic number theory and also physics (Fermion operator has very
    concrete realization with MÃ¶bius Function model).

    Parameters
    ==========

    n : positive integer

    Examples
    ========

    >>> from sympy.ntheory import mobius
    >>> mobius(13*7)
    1
    >>> mobius(1)
    1
    >>> mobius(13*7*5)
    -1
    >>> mobius(13**2)
    0

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/M%C3%B6bius_function
    .. [2] Thomas Koshy "Elementary Number Theory with Applications"

    c         C  s©   | j  r* | j t k	 r6 t d ƒ ‚ q6 n t d ƒ ‚ | j rF t j S| t j k r\ t j S| j	 r¥ t
 | ƒ } t d „  | j ƒ  Dƒ ƒ r” t j St j t | ƒ Sd  S(   Ns   n should be a positive integers   n should be an integerc         s  s   |  ] } | d  k Vq d S(   i   N(    (   RH   R'   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pys	   <genexpr>÷  s    (   t
   is_integert   is_positiveRK   R   t	   TypeErrort   is_primeR	   t   NegativeOnet   Onet
   is_IntegerR   t   anyt   valuest   ZeroR+   (   t   clsR   R   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   evalê  s    			(   t   __name__t
   __module__t   __doc__t   classmethodR©   (    (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyR   Â  s   'c         C  st   | |  ;} | |  ;} | d k r) |  } n  d } x2 t | ƒ D]$ } | | k rR | S| | |  } q< Wt d ƒ ‚ d S(   s‡  
    Trial multiplication algorithm for computing the discrete logarithm of
    ``a`` to the base ``b`` modulo ``n``.

    The algorithm finds the discrete logarithm using exhaustive search. This
    naive method is used as fallback algorithm of ``discrete_log`` when the
    group order is very small.

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import _discrete_log_trial_mul
    >>> _discrete_log_trial_mul(41, 15, 7)
    3

    See Also
    ========

    discrete_log

    References
    ==========

    .. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
        Vanstone, S. A. (1997).
    i   s   Log does not existN(   R,   R   R   (   R   R   Ri   R!   R=   R'   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _discrete_log_trial_mulü  s    

	c   	      C  sø   | |  ;} | |  ;} | d k r2 t | |  ƒ } n  t | ƒ d } t ƒ  } d } x, t | ƒ D] } | | | <| | |  } q^ Wt | |  ƒ } t | | |  ƒ } | } x> t | ƒ D]0 } | | k rÖ | | | | S| | |  } q´ Wt d ƒ ‚ d S(   se  
    Baby-step giant-step algorithm for computing the discrete logarithm of
    ``a`` to the base ``b`` modulo ``n``.

    The algorithm is a time-memory trade-off of the method of exhaustive
    search. It uses `O(sqrt(m))` memory, where `m` is the group order.

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import _discrete_log_shanks_steps
    >>> _discrete_log_shanks_steps(41, 15, 7)
    3

    See Also
    ========

    discrete_log

    References
    ==========

    .. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
        Vanstone, S. A. (1997).
    i   s   Log does not existN(   R,   R%   R   t   dictR   R   R   R   (	   R   R   Ri   R!   R;   t   TR=   R'   R‘   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _discrete_log_shanks_steps#  s$    

	
i
   c         C  s  | |  ;} | |  ;} | d k r2 t | |  ƒ } n  t ƒ  } | d k	 rW | j | ƒ n  x&t | ƒ D]} | j d | d ƒ } | j d | d ƒ }	 t | | |  ƒ t | |	 |  ƒ |  }
 |
 d } | d k r÷ | |
 |  } | } |	 d | } n[ | d k r0|
 |
 |  } | | | } |	 |	 | } n" | |
 |  } | d | } |	 } x't | ƒ D]} |
 d } | d k rš| |
 |  }
 |	 d | }	 nU | d k rÓ|
 |
 |  }
 | | | } |	 |	 | }	 n | |
 |  }
 | d | } | d } | d k r$| | |  } | d | } nU | d k r]| | |  } | | | } | | | } n | | |  } | d | } | d } | d k r®| | |  } | d | } nU | d k rç| | |  } | | | } | | | } n | | |  } | d | } |
 | k r_|	 | | } yC t | | ƒ | | | } t | | |  ƒ | |  d k r_| SWn t k
 rsn XPq_q_Wqd Wt d ƒ ‚ d S(   sg  
    Pollard's Rho algorithm for computing the discrete logarithm of ``a`` to
    the base ``b`` modulo ``n``.

    It is a randomized algorithm with the same expected running time as
    ``_discrete_log_shanks_steps``, but requires a negligible amount of memory.

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import _discrete_log_pollard_rho
    >>> _discrete_log_pollard_rho(227, 3**7, 3)
    7

    See Also
    ========

    discrete_log

    References
    ==========

    .. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
        Vanstone, S. A. (1997).
    i   i   i    s&   Pollard's Rho failed to find logarithmN(	   R,   R%   R   t   seedR   R   R   R   R   (   R   R   Ri   R!   t   retriest   rseedt   prngR'   t   aat   bat   xaR†   t   xbt   abt   bbRu   R8   R#   (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _discrete_log_pollard_rhoQ  s€    

	&



 c         C  sd  d d l  m } | |  ;} | |  ;} | d k rB t | |  ƒ } n  t | ƒ } d g t | ƒ } xÄ t | j ƒ  ƒ D]° \ } \ } }	 x› t |	 ƒ D] }
 t	 | | | |  ƒ } t	 | t
 | |  ƒ | | |
 d |  ƒ } t	 | | | |  ƒ } t |  | | | t ƒ } | | c | | |
 7<q“ Wqt W| g  | j ƒ  D] \ } }	 | |	 ^ q8| ƒ \ } } | S(   s¡  
    Pohlig-Hellman algorithm for computing the discrete logarithm of ``a`` to
    the base ``b`` modulo ``n``.

    In order to compute the discrete logarithm, the algorithm takes advantage
    of the factorization of the group order. It is more efficient when the
    group order factors into many small primes.

    Examples
    ========

    >>> from sympy.ntheory.residue_ntheory import _discrete_log_pohlig_hellman
    >>> _discrete_log_pohlig_hellman(251, 210, 71)
    197

    See Also
    ========

    discrete_log

    References
    ==========

    .. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
        Vanstone, S. A. (1997).
    i   (   t   crti    N(   t   modularR½   R,   R%   R   R+   RG   R   R   R   R   RŽ   RK   (   R   R   Ri   R!   R½   R   t   lR'   t   pit   riRu   t   gjt   ajt   bjt   cjR7   t   _(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   _discrete_log_pohlig_hellman¸  s     

%+ 8c         C  sÉ   t  |  ƒ t  | ƒ t  | ƒ }  } } | d k rD t | |  ƒ } n  | d k r_ t | ƒ } n  | d k  r~ t |  | | | ƒ S| r¶ | d k  r£ t |  | | | ƒ St |  | | | ƒ St |  | | | ƒ S(   s  
    Compute the discrete logarithm of ``a`` to the base ``b`` modulo ``n``.

    This is a recursive function to reduce the discrete logarithm problem in
    cyclic groups of composite order to the problem in cyclic groups of prime
    order.

    It employs different algorithms depending on the problem (subgroup order
    size, prime order or not):

        * Trial multiplication
        * Baby-step giant-step
        * Pollard's Rho
        * Pohlig-Hellman

    Examples
    ========

    >>> from sympy.ntheory import discrete_log
    >>> discrete_log(41, 15, 7)
    3

    References
    ==========

    .. [1] http://mathworld.wolfram.com/DiscreteLogarithm.html
    .. [2] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., &
        Vanstone, S. A. (1997).

    iè  I ¥Ôè   N(   R   R,   R%   R
   R®   R±   R¼   RÇ   (   R   R   Ri   R!   t   prime_order(    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyRŽ   é  s    &N(7   t
   __future__R    R   t   sympy.core.compatibilityR   R   t   sympy.core.functionR   Rb   R   R   R   t   sympy.core.powerR   t   sympy.core.singletonR	   t	   primetestR
   t   factor_R   R   R   R   t   randomR   R   R%   R*   R/   R-   R>   RL   RE   RR   R   R@   RY   RX   Rc   R   R€   R‚   RŒ   R‰   R›   Rœ   R4   R   R   R,   R®   R±   R¼   RÇ   RŽ   (    (    (    s<   lib/python2.7/site-packages/sympy/ntheory/residue_ntheory.pyt   <module>   sD   "	+		B		 4	"<	u	i	$				
	.@		.	X:'.g1