
\c           @  s2  d  Z  d d l m Z m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z m Z d d	 l m Z d
   Z d   Z d   Z d f  d     YZ e   Z d   Z d e	 f d     YZ d d  Z d   Z d   Z d   Z e d  Z e e d  Z d   Z  d   Z! d S(   s"   
Generating and counting primes.

i(   t   print_functiont   divisionN(   t   bisect(   t   array(   t   Functiont   S(   t   as_intt   rangei   (   t   isprimec         C  s   t  d d g |   S(   Nt   li    (   t   _array(   t   n(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   _azeros   s    c          G  s   t  d |   S(   NR	   (   R
   (   t   v(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   _aset   s    c         C  s   t  d t |  |   S(   NR	   (   R
   R   (   t   at   b(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   _arange   s    t   Sievec           B  sz   e  Z d  Z d   Z d   Z d d d d  Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z RS(   s  An infinite list of prime numbers, implemented as a dynamically
    growing sieve of Eratosthenes. When a lookup is requested involving
    an odd number that has not been sieved, the sieve is automatically
    extended up to that number.

    Examples
    ========

    >>> from sympy import sieve
    >>> sieve._reset() # this line for doctest only
    >>> 25 in sieve
    False
    >>> sieve._list
    array('l', [2, 3, 5, 7, 11, 13, 17, 19, 23])
    c           s   d   _  t d d d d d d    _ t d d	 d	 d d d
    _ t d d	 d d d d    _ t   f d     j   j   j f D  s t  d  S(   Ni   i   i   i   i   i   i   i    i   i   ic         3  s$   |  ] } t  |    j k Vq d  S(   N(   t   lent   _n(   t   .0t   i(   t   self(    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pys	   <genexpr>5   s    (   R   R   t   _listt   _tlistt   _mlistt   allt   AssertionError(   R   (    (   R   s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   __init__0   s
    	c         C  s   d d t  |  j  |  j d |  j d |  j d |  j d |  j d d t  |  j  |  j d |  j d |  j d |  j d |  j d d	 t  |  j  |  j d |  j d |  j d |  j d |  j d f S(
   Nss   <%s sieve (%i): %i, %i, %i, ... %i, %i
%s sieve (%i): %i, %i, %i, ... %i, %i
%s sieve (%i): %i, %i, %i, ... %i, %i>t   primei    i   i   iit   totientt   mobius(   R   R   R   R   (   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   __repr__7   s    c         C  s   t  d   | | | f D  r0 t } } } n  | rL |  j |  j  |  _ n  | rh |  j |  j  |  _ n  | r |  j |  j  |  _ n  d S(   s]   Reset all caches (default). To reset one or more set the
            desired keyword to True.c         s  s   |  ] } | d  k Vq d  S(   N(   t   None(   R   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pys	   <genexpr>H   s    N(   R   t   TrueR   R   R   R   (   R   R   R   R    (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   _resetE   s    c   	      C  s   t  |  } | |  j d k r# d St  | d  d } |  j |  |  j d d } t | | d  } xR |  j d |  D]> } | | } x* t | t |  |  D] } d | | <q Wq{ W|  j t d g  | D] } | r | ^ q  7_ d S(   s  Grow the sieve to cover all primes <= n (a real number).

        Examples
        ========

        >>> from sympy import sieve
        >>> sieve._reset() # this line for doctest only
        >>> sieve.extend(30)
        >>> sieve[10] == 29
        True
        iNg      ?i   i   i    R	   (   t   intR   t   extendR   t
   primerangeR   R   R
   (	   R   R   t   maxbaset   begint   newsievet   pt
   startindexR   t   x(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR&   Q   s    c         C  sJ   t  |  } x7 t |  j  | k  rE |  j t |  j d d   q Wd S(   s  Extend to include the ith prime number.

        Parameters
        ==========

        i : integer

        Examples
        ========

        >>> from sympy import sieve
        >>> sieve._reset() # this line for doctest only
        >>> sieve.extend_to_no(9)
        >>> sieve._list
        array('l', [2, 3, 5, 7, 11, 13, 17, 19, 23])

        Notes
        =====

        The list is extended by 50% if it is too short, so it is
        likely that it will be longer than requested.
        ig      ?N(   R   R   R   R&   R%   (   R   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   extend_to_nov   s    c         c  s   d d l  m } t d t | |    } t | |   } | | k rM d S|  j |  |  j |  d } t |  j  d } xC | | k  r |  j | d } | | k  r | V| d 7} q d Sq Wd S(   s   Generate all prime numbers in the range [a, b).

        Examples
        ========

        >>> from sympy import sieve
        >>> print([i for i in sieve.primerange(7, 18)])
        [7, 11, 13, 17]
        i(   t   ceilingi   Ni   (   t#   sympy.functions.elementary.integersR/   t   maxR   R&   t   searchR   R   (   R   R   R   R/   R   t   maxiR+   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR'      s    
c   	      c  s  d d l  m } t d t | |    } t | |   } t |  j  } | | k r\ d S| | k r x.t | |  D] } |  j | Vqx Wn|  j t | |  7_ x~ t d |  D]m } |  j | } | | d | | } x- t | | |  D] } |  j | c | 8<q W| | k r | Vq q Wxl t | |  D][ } |  j | } x1 t d | | |  D] } |  j | c | 8<qdW| | k r:| Vq:q:Wd S(   s   Generate all totient numbers for the range [a, b).

        Examples
        ========

        >>> from sympy import sieve
        >>> print([i for i in sieve.totientrange(7, 18)])
        [6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16]
        i(   R/   i   Ni   (   R0   R/   R1   R   R   R   R   R   (	   R   R   R   R/   R   R   t   tiR,   t   j(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   totientrange   s.    
c   	      c  s  d d l  m } t d t | |    } t | |   } t |  j  } | | k r\ d S| | k r x/t | |  D] } |  j | Vqx Wn	|  j t | |  7_ x~ t d |  D]m } |  j | } | | d | | } x- t | | |  D] } |  j | c | 8<q W| | k r | Vq q Wxl t | |  D][ } |  j | } x1 t d | | |  D] } |  j | c | 8<qeW| | k r;| Vq;q;Wd S(   s  Generate all mobius numbers for the range [a, b).

        Parameters
        ==========

        a : integer
            First number in range

        b : integer
            First number outside of range

        Examples
        ========

        >>> from sympy import sieve
        >>> print([i for i in sieve.mobiusrange(7, 18)])
        [-1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1]
        i(   R/   i   Ni   (   R0   R/   R1   R   R   R   R   R   (	   R   R   R   R/   R   R   t   miR,   R5   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   mobiusrange   s.    c         C  s   d d l  m } t | |   } t |  } | d k  rM t d |   n  | |  j d k rp |  j |  n  t |  j |  } |  j | d | k r | | f S| | d f Sd S(   s~  Return the indices i, j of the primes that bound n.

        If n is prime then i == j.

        Although n can be an expression, if ceiling cannot convert
        it to an integer then an n error will be raised.

        Examples
        ========

        >>> from sympy import sieve
        >>> sieve.search(25)
        (9, 10)
        >>> sieve.search(23)
        (9, 9)
        i(   R/   i   s   n should be >= 2 but got: %si   N(   R0   R/   R   t
   ValueErrorR   R&   R   (   R   R   R/   t   testR   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR2     s    
c         C  sv   y" t  |  } | d k s! t  Wn t t f k
 r< t SX| d d k rW | d k S|  j |  \ } } | | k S(   Ni   i    (   R   R   R9   t   FalseR2   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   __contains__&  s    
c         C  sp   t  | t  rD |  j | j  |  j | j d | j d | j  St |  } |  j |  |  j | d Sd S(   s   Return the nth prime numberi   N(   t
   isinstancet   sliceR.   t   stopR   t   startt   stepR   (   R   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   __getitem__1  s    %N(   t   __name__t
   __module__t   __doc__R   R!   R"   R$   R&   R.   R'   R6   R8   R2   R<   RB   (    (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR      s   			%			'	0	!	c         C  s$  t  |   } | d k  r' t d   n  | t t j  k rD t | Sd d l m } d d l m } d } t	 | | |  | | |    } xC | | k  r | | d ?} | |  | k r | } q | d } q Wt
 | d  } x3 | | k  rt |  r| d 7} n  | d 7} q W| d S(   sD   Return the nth prime, with the primes indexed as prime(1) = 2,
        prime(2) = 3, etc.... The nth prime is approximately n*log(n).

        Logarithmic integral of x is a pretty nice approximation for number of
        primes <= x, i.e.
        li(x) ~ pi(x)
        In fact, for the numbers we are concerned about( x<1e11 ),
        li(x) - pi(x) < 50000

        Also,
        li(x) > pi(x) can be safely assumed for the numbers which
        can be evaluated by this function.

        Here, we find the least integer m such that li(m) > n using binary search.
        Now pi(m-1) < li(m-1) <= n,

        We find pi(m - 1) using primepi function.

        Starting from m, we have to find n - pi(m-1) more primes.

        For the inputs this implementation can handle, we will have to test
        primality for at max about 10**5 numbers, to get our answer.

        Examples
        ========

        >>> from sympy import prime
        >>> prime(10)
        29
        >>> prime(1)
        2
        >>> prime(100000)
        1299709

        See Also
        ========

        sympy.ntheory.primetest.isprime : Test if n is prime
        primerange : Generate all primes in a given range
        primepi : Return the number of primes less than or equal to n

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Prime_number_theorem#Table_of_.CF.80.28x.29.2C_x_.2F_log_x.2C_and_li.28x.29
        .. [2] https://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number
        .. [3] https://en.wikipedia.org/wiki/Skewes%27_number
    i   s-   nth must be a positive integer; prime(1) == 2i(   t   li(   t   logi   (   R   R9   R   t   sieveR   t'   sympy.functions.special.error_functionsRF   t&   sympy.functions.elementary.exponentialRG   R%   t   primepiR   (   t   nthR   RF   RG   R   R   t   midt   n_primes(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR   ?  s(    1&	RK   c           B  s   e  Z d  Z e d    Z RS(   s:   Represents the prime counting function pi(n) = the number
        of prime numbers less than or equal to n.

        Algorithm Description:

        In sieve method, we remove all multiples of prime p
        except p itself.

        Let phi(i,j) be the number of integers 2 <= k <= i
        which remain after sieving from primes less than
        or equal to j.
        Clearly, pi(n) = phi(n, sqrt(n))

        If j is not a prime,
        phi(i,j) = phi(i, j - 1)

        if j is a prime,
        We remove all numbers(except j) whose
        smallest prime factor is j.

        Let x= j*a be such a number, where 2 <= a<= i / j
        Now, after sieving from primes <= j - 1,
        a must remain
        (because x, and hence a has no prime factor <= j - 1)
        Clearly, there are phi(i / j, j - 1) such a
        which remain on sieving from primes <= j - 1

        Now, if a is a prime less than equal to j - 1,
        x= j*a has smallest prime factor = a, and
        has already been removed(by sieving from a).
        So, we don't need to remove it again.
        (Note: there will be pi(j - 1) such x)

        Thus, number of x, that will be removed are:
        phi(i / j, j - 1) - phi(j - 1, j - 1)
        (Note that pi(j - 1) = phi(j - 1, j - 1))

        => phi(i,j) = phi(i, j - 1) - phi(i / j, j - 1) + phi(j - 1, j - 1)

        So,following recursion is used and implemented as dp:

        phi(a, b) = phi(a, b - 1), if b is not a prime
        phi(a, b) = phi(a, b-1)-phi(a / b, b-1) + phi(b-1, b-1), if b is prime

        Clearly a is always of the form floor(n / k),
        which can take at most 2*sqrt(n) values.
        Two arrays arr1,arr2 are maintained
        arr1[i] = phi(i, j),
        arr2[i] = phi(n // i, j)

        Finally the answer is arr2[1]

        Examples
        ========

        >>> from sympy import primepi
        >>> primepi(25)
        9

        See Also
        ========

        sympy.ntheory.primetest.isprime : Test if n is prime
        primerange : Generate all primes in a given range
        prime : Return the nth prime
    c   
      C  s  | t  j k r t  j S| t  j k r, t  j Sy t |  } Wn? t k
 r} | j t k sj | t  j k ry t	 d   n  d  SX| d k  r t  j S| t
 j d k r t  t
 j |  d  St | d  } | d 8} t | d  } x | | | k r| d 7} q W| d 8} d g | d } d g | d } x; t d | d  D]& } | d | | <| | d | | <qEWxt d | d  D] } | | | | d k rqn  | | d } xy t d t | | | |  d  D]S } | | } | | k r| | c | | | 8<q| | c | | | | 8<qWt | | | d  }	 x6 t | |	 d  D]" } | | c | | | | 8<q[WqWt  | d  S(   Ns   n must be reali   ii    g      ?i   (   R   t   Infinityt   NegativeInfinityt   ZeroR%   t	   TypeErrort   is_realR;   t   NaNR9   RH   R   R2   R1   R   t   min(
   t   clsR   t   limt   arr1t   arr2R   R+   R5   t   stt   lim2(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   eval  sL    

+
 $(   RC   RD   RE   t   classmethodR\   (    (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyRK     s   Bc         C  s  t  |   }  t |  } | d k ra |  } d } x* t |  } | d 7} | | k r3 Pq3 q3 W| S|  d k  rq d S|  d k  r i d d 6d d 6d d 6d d 6d d 6|  S|  t j d k r t j |   \ } } | | k r t | d St | Sn  d |  d } | |  k r4|  d 7}  t |   r'|  S|  d 7}  nA |  | d k rk|  d 7}  t |   r^|  S|  d 7}  n
 | d }  x8 t |   r|  S|  d 7}  t |   r|  S|  d 7}  qxWd	 S(
   sB   Return the ith prime greater than n.

        i must be an integer.

        Notes
        =====

        Potential primes are located at 6*j +/- 1. This
        property is used during searching.

        >>> from sympy import nextprime
        >>> [(i, nextprime(i)) for i in range(10, 15)]
        [(10, 11), (11, 13), (12, 13), (13, 17), (14, 17)]
        >>> nextprime(2, ith=2) # the 2nd prime after 2
        5

        See Also
        ========

        prevprime : Return the largest prime smaller than n
        primerange : Generate all primes in a given range

    i   i   i   i   i   i   i   iN(   R%   R   t	   nextprimeRH   R   R2   R   (   R   t   ithR   t   prR5   R	   t   ut   nn(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR^     sN    
+



c         C  sM  d d l  m } t | |    }  |  d k  r= t d   n  |  d k  rt i d d 6d d 6d d 6d d	 6d d
 6|  S|  t j d k r t j |   \ } } | | k r t | d St | Sn  d	 |  d	 } |  | d k r| d }  t |   r |  S|  d 8}  n
 | d }  x8 t |   r!|  S|  d 8}  t |   r;|  S|  d 8}  qWd S(   s   Return the largest prime smaller than n.

        Notes
        =====

        Potential primes are located at 6*j +/- 1. This
        property is used during searching.

        >>> from sympy import prevprime
        >>> [(i, prevprime(i)) for i in range(10, 15)]
        [(10, 7), (11, 7), (12, 11), (13, 11), (14, 13)]

        See Also
        ========

        nextprime : Return the ith prime greater than n
        primerange : Generates all primes in a given range
    i(   R/   i   s   no preceding primesi   i   i   i   i   i   i   N(   R0   R/   R   R9   RH   R   R2   R   (   R   R/   R	   Ra   Rb   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt	   prevprime@  s2    +


c         c  s   d d l  m } |  | k r  d S| t j d k rY x t j |  |  D] } | VqF Wd St | |    d }  t | |   } x( t |   }  |  | k  r |  Vq d Sq Wd S(   s   Generate a list of all prime numbers in the range [a, b).

        If the range exists in the default sieve, the values will
        be returned from there; otherwise values will be returned
        but will not modify the sieve.

        Examples
        ========

        >>> from sympy import primerange, sieve
        >>> print([i for i in primerange(1, 30)])
        [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

        The Sieve method, primerange, is generally faster but it will
        occupy more memory as the sieve stores values. The default
        instance of Sieve, named sieve, can be used:

        >>> list(sieve.primerange(1, 30))
        [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

        Notes
        =====

        Some famous conjectures about the occurrence of primes in a given
        range are [1]:

        - Twin primes: though often not, the following will give 2 primes
                    an infinite number of times:
                        primerange(6*n - 1, 6*n + 2)
        - Legendre's: the following always yields at least one prime
                        primerange(n**2, (n+1)**2+1)
        - Bertrand's (proven): there is always a prime in the range
                        primerange(n, 2*n)
        - Brocard's: there are at least four primes in the range
                        primerange(prime(n)**2, prime(n+1)**2)

        The average gap between primes is log(n) [2]; the gap between
        primes can be arbitrarily large since sequences of composite
        numbers are arbitrarily large, e.g. the numbers in the sequence
        n! + 2, n! + 3 ... n! + n are all composite.

        See Also
        ========

        nextprime : Return the ith prime greater than n
        prevprime : Return the largest prime smaller than n
        randprime : Returns a random prime in a given range
        primorial : Returns the product of primes based on condition
        Sieve.primerange : return range from already computed primes
                           or extend the sieve to contain the requested
                           range.

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Prime_number
        .. [2] http://primes.utm.edu/notes/gaps.html
    i(   R/   Ni   (   R0   R/   RH   R   R'   R   R^   (   R   R   R/   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyR'   s  s    ;	c         C  s   |  | k r d St  t |  | f  \ }  } t j |  d |  } t |  } | | k rh t |  } n  | |  k  r t d   n  | S(   s$   Return a random prime number in the range [a, b).

        Bertrand's postulate assures that
        randprime(a, 2*a) will always succeed for a > 1.

        Examples
        ========

        >>> from sympy import randprime, isprime
        >>> randprime(1, 30) #doctest: +SKIP
        13
        >>> isprime(randprime(1, 30))
        True

        See Also
        ========

        primerange : Generate all primes in a given range

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Bertrand's_postulate

    Ni   s&   no primes exist in the specified range(   t   mapR%   t   randomt   randintR^   Rc   R9   (   R   R   R   R+   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt	   randprime  s    c         C  s   | r t  |   }  n t |   }  |  d k  r< t d   n  d } | ry xV t d |  d  D] } | t |  9} q\ Wn( x% t d |  d  D] } | | 9} q W| S(   sE  
    Returns the product of the first n primes (default) or
    the primes less than or equal to n (when ``nth=False``).

    Examples
    ========

    >>> from sympy.ntheory.generate import primorial, randprime, primerange
    >>> from sympy import factorint, Mul, primefactors, sqrt
    >>> primorial(4) # the first 4 primes are 2, 3, 5, 7
    210
    >>> primorial(4, nth=False) # primes <= 4 are 2 and 3
    6
    >>> primorial(1)
    2
    >>> primorial(1, nth=False)
    1
    >>> primorial(sqrt(101), nth=False)
    210

    One can argue that the primes are infinite since if you take
    a set of primes and multiply them together (e.g. the primorial) and
    then add or subtract 1, the result cannot be divided by any of the
    original factors, hence either 1 or more new primes must divide this
    product of primes.

    In this case, the number itself is a new prime:

    >>> factorint(primorial(4) + 1)
    {211: 1}

    In this case two new primes are the factors:

    >>> factorint(primorial(4) - 1)
    {11: 1, 19: 1}

    Here, some primes smaller and larger than the primes multiplied together
    are obtained:

    >>> p = list(primerange(10, 20))
    >>> sorted(set(primefactors(Mul(*p) + 1)).difference(set(p)))
    [2, 5, 31, 149]

    See Also
    ========

    primerange : Generate all primes in a given range

    i   s   primorial argument must be >= 1i   (   R   R%   R9   R   R   R'   (   R   RL   R+   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt	   primorial  s    2c   
      c  so  t  | p d  } d } } | |  |  } } d } xv | | k r | sW | | k  r | d 7} | | k r | } | d 9} d } n  | r | Vn  |  |  } | d 7} q8 W| r | | k r | r d S| d f Vd Sn  | skd }	 | } } x  t |  D] } |  |  } q Wx2 | | k rI|  |  } |  |  } |	 d 7}	 qW|	 r]|	 d 8}	 n  | |	 f Vn  d S(   s  For a given iterated sequence, return a generator that gives
    the length of the iterated cycle (lambda) and the length of terms
    before the cycle begins (mu); if ``values`` is True then the
    terms of the sequence will be returned instead. The sequence is
    started with value ``x0``.

    Note: more than the first lambda + mu terms may be returned and this
    is the cost of cycle detection with Brent's method; there are, however,
    generally less terms calculated than would have been calculated if the
    proper ending point were determined, e.g. by using Floyd's method.

    >>> from sympy.ntheory.generate import cycle_length

    This will yield successive values of i <-- func(i):

        >>> def iter(func, i):
        ...     while 1:
        ...         ii = func(i)
        ...         yield ii
        ...         i = ii
        ...

    A function is defined:

        >>> func = lambda i: (i**2 + 1) % 51

    and given a seed of 4 and the mu and lambda terms calculated:

        >>> next(cycle_length(func, 4))
        (6, 2)

    We can see what is meant by looking at the output:

        >>> n = cycle_length(func, 4, values=True)
        >>> list(ni for ni in n)
        [17, 35, 2, 5, 26, 14, 44, 50, 2, 5, 26, 14]

    There are 6 repeating values after the first 2.

    If a sequence is suspected of being longer than you might wish, ``nmax``
    can be used to exit early (and mu will be returned as None):

        >>> next(cycle_length(func, 4, nmax = 4))
        (4, None)
        >>> [ni for ni in cycle_length(func, 4, nmax = 4, values=True)]
        [17, 35, 2, 5]

    Code modified from:
        https://en.wikipedia.org/wiki/Cycle_detection.
    i    i   i   N(   R%   R"   R   (
   t   ft   x0t   nmaxt   valuest   powert   lamt   tortoiset   hareR   t   mu(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   cycle_length-  s>    4
"

	
c   	   
   C  s  t  |   } | d k  r' t d   n  d d d d d d d	 d
 d d g
 } | d k rc | | d Sd t j d } } | | t |  d k r xK | | d k  r | | d ?} | t |  d | k r | } q | } q Wt |  r | d 8} n  | Sd d l m } d d l m	 } d } t
 | | |  | | |    } xK | | k  r| | d ?} | | |  d | k r| } qK| d } qKW| t |  d } x3 | | k rt |  s| d 8} n  | d 8} qWt |  r| d 8} n  | S(   s   Return the nth composite number, with the composite numbers indexed as
        composite(1) = 4, composite(2) = 6, etc....

        Examples
        ========

        >>> from sympy import composite
        >>> composite(36)
        52
        >>> composite(1)
        4
        >>> composite(17737)
        20000

        See Also
        ========

        sympy.ntheory.primetest.isprime : Test if n is prime
        primerange : Generate all primes in a given range
        primepi : Return the number of primes less than or equal to n
        prime : Return the nth prime
        compositepi : Return the number of positive composite numbers less than or equal to n
    i   s1   nth must be a positive integer; composite(1) == 4i   i   i   i	   i
   i   i   i   i   i   i(   RF   (   RG   (   R   R9   RH   R   RK   R   RI   RF   RJ   RG   R%   (	   RL   R   t   composite_arrR   R   RM   RF   RG   t   n_composites(    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt	   composite  sB    $	
&	c         C  s.   t  |   }  |  d k  r d S|  t |   d S(   sk   Return the number of positive composite numbers less than or equal to n.
        The first positive composite is 4, i.e. compositepi(4) = 1.

        Examples
        ========

        >>> from sympy import compositepi
        >>> compositepi(25)
        15
        >>> compositepi(1000)
        831

        See Also
        ========

        sympy.ntheory.primetest.isprime : Test if n is prime
        primerange : Generate all primes in a given range
        prime : Return the nth prime
        primepi : Return the number of primes less than or equal to n
        composite : Return the nth composite number
    i   i    i   (   R%   RK   (   R   (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   compositepi  s    ("   RE   t
   __future__R    R   Re   R   R   R
   t   sympyR   R   t   sympy.core.compatibilityR   R   t	   primetestR   R   R   R   R   RH   R   RK   R^   Rc   R'   Rg   R#   Rh   R"   R;   Rr   Ru   Rv   (    (    (    s5   lib/python2.7/site-packages/sympy/ntheory/generate.pyt   <module>   s.   			 		KrD	3	R	&BY	B