
    Edr                        d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ d Zd Zd Zd Z G d d          Z e            Zd Z G d de          ZddZd ZddZd Zd dZd!dZd Zd ZdS )"z"
Generating and counting primes.

    N)bisectcount)array)Function)S   )isprime)as_intc                 *    t          ddg| z            S )Nlr   _arrayns    6lib/python3.11/site-packages/sympy/ntheory/generate.py_azerosr      s    #s1u    c                  "    t          d|           S Nr   r   )vs    r   _asetr      s    #q>>r   c                 >    t          dt          | |                    S r   )r   rangeabs     r   _aranger      s    #uQ{{###r   c                 >    ddl m} t           ||                     S )z Wrapping ceiling in as_int will raise an error if there was a problem
        determining whether the expression was exactly an integer or not.r   )ceiling)#sympy.functions.elementary.integersr    r   )r   r    s     r   _as_int_ceilingr"      s,     <;;;;;''!**r   c                   ^    e Zd ZdZd Zd ZddZd Zd ZddZ	d	 Z
d
 Zd Zd Zd Zd ZdS )Sievea  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                 
    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J d S )N                     r   r	      c              3   H   K   | ]}t          |          j        k    V  d S N)len_n).0iselfs     r   	<genexpr>z!Sieve.__init__.<locals>.<genexpr>=   s0      UU3q66TW$UUUUUUr   )r2   r   _list_tlist_mlistallr5   s   `r   __init__zSieve.__init__8   s    1aAr2..
Aq!Q1--Aq"b!R00UUUUtz4;.TUUUUUUUUUUr   c                    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z  S )
Nzs<%s sieve (%i): %i, %i, %i, ... %i, %i
%s sieve (%i): %i, %i, %i, ... %i, %i
%s sieve (%i): %i, %i, %i, ... %i, %i>primer   r	   r'   r.   totientmobius)r1   r7   r8   r9   r;   s    r   __repr__zSieve.__repr__?   s    6 c$*ooA
1tz!}BBDK((QQQR$+b/s4;''QQQR$+b/	:CC 	Cr   Nc                     t          d |||fD                       rdx}x}}|r| j        d| j                 | _        |r| j        d| j                 | _        |r| j        d| j                 | _        dS dS )z]Reset all caches (default). To reset one or more set the
            desired keyword to True.c              3      K   | ]}|d u V  	d S r0    )r3   r4   s     r   r6   zSieve._reset.<locals>.<genexpr>P   s&      ;;QqDy;;;;;;r   TN)r:   r7   r2   r8   r9   )r5   r>   r@   rA   s       r   _resetzSieve._resetM   s     ;;5'6":;;;;; 	,'++E+Gf 	.HTWH-DJ 	0+htwh/DK 	0+htwh/DKKK	0 	0r   c                    t          |          }|| j        d         k    rdS t          |dz            dz   }|                     |           | j        d         dz   }t          ||dz             }|                     |          D ].}| |z  }t          |t          |          |          D ]}d||<   /| xj        t          dd |D                       z  c_        dS )a  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
        r.   N      ?r	   r   r   c                     g | ]}||S rE   rE   )r3   xs     r   
<listcomp>z Sieve.extend.<locals>.<listcomp>|   s    "<"<"<!"<1"<"<"<r   )intr7   extendr   
primeranger   r1   r   )r5   r   maxbasebeginnewsievep
startindexr4   s           r   rM   zSieve.extendY   s     FF
2 	F
 af++/G 
2"5!a%(( )) 	  	 A !&AJ:s8}}a88      	

fS"<"<h"<"<"<===



r   c                     t          |          }t          | j                  |k     rJ|                     t	          | j        d         dz                       t          | j                  |k     HdS dS )a  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.
        r.   g      ?N)r   r1   r7   rM   rL   )r5   r4   s     r   extend_to_nozSieve.extend_to_no~   st    . 1II$*oo! 	3KKDJrNS011222 $*oo! 	3 	3 	3 	3 	3r   c              #     K   |t          |          }d}n,t          dt          |                    }t          |          }||k    rdS |                     |           |                     |          d         }t	          | j                  dz   }||k     r*| j        |dz
           }||k     r
|V  |dz  }ndS ||k     (dS dS )a(  Generate all prime numbers in the range [2, a) or [a, b).

        Examples
        ========

        >>> from sympy import sieve, prime

        All primes less than 19:

        >>> print([i for i in sieve.primerange(19)])
        [2, 3, 5, 7, 11, 13, 17]

        All primes greater than or equal to 7 and less than 19:

        >>> print([i for i in sieve.primerange(7, 19)])
        [7, 11, 13, 17]

        All primes through the 10th prime

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

        Nr'   r	   )r"   maxrM   searchr1   r7   )r5   r   r   r4   maxirR   s         r   rN   zSieve.primerange   s      2  	#""AAAAq))**A""A6 	FAKKNN14:"$h 	
1q5!A1u Q $h 	 	 	 	 	r   c              #     K   t          dt          |                    }t          |          }t          | j                  }||k    rdS ||k    r$t	          ||          D ]}| j        |         V  dS | xj        t          ||          z  c_        t	          d|          D ]P}| j        |         }||z   dz
  |z  |z  }t	          |||          D ]}| j        |xx         |z  cc<   ||k    r|V  Qt	          ||          D ]E}| j        |         }t	          d|z  ||          D ]}| j        |xx         |z  cc<   ||k    r|V  FdS )zGenerate 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]
        r	   Nr'   )rW   r"   r1   r8   r   r   )r5   r   r   r   r4   tirS   js           r   totientrangezSieve.totientrange   s      ?1%%&&A6 	F!V 	1a[[ % %k!n$$$$% % KK71a==(KK1a[[  [^!eaiA-1
z1a00 ) )AKNNNb(NNNN6 HHH1a[[  [^q1ua++ ) )AKNNNb(NNNN6 HHH r   c              #     K   t          dt          |                    }t          |          }t          | j                  }||k    rdS ||k    r$t	          ||          D ]}| j        |         V  dS | xj        t          ||z
            z  c_        t	          d|          D ]P}| j        |         }||z   dz
  |z  |z  }t	          |||          D ]}| j        |xx         |z  cc<   ||k    r|V  Qt	          ||          D ]E}| j        |         }t	          d|z  ||          D ]}| j        |xx         |z  cc<   ||k    r|V  FdS )a  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]
        r	   Nr'   )rW   r"   r1   r9   r   r   )r5   r   r   r   r4   mirS   r\   s           r   mobiusrangezSieve.mobiusrange   s     & ?1%%&&A6 	F!V 	1a[[ % %k!n$$$$% % KK71q5>>)KK1a[[  [^!eaiA-1
z1a00 ) )AKNNNb(NNNN6 HHH1a[[  [^q1ua++ ) )AKNNNb(NNNN6 HHH r   c                 "   t          |          }t          |          }|dk     rt          d|z            || j        d         k    r|                     |           t          | j        |          }| j        |dz
           |k    r||fS ||dz   fS )a~  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)
        r'   zn should be >= 2 but got: %sr.   r	   )r"   r   
ValueErrorr7   rM   r   )r5   r   testr   s       r   rX   zSieve.search  s    " q!!1IIq5 	A;a?@@@tz"~ 	KKNNN4:q!!:a!e$ 	a4Ka!e8Or   c                     	 t          |          }|dk    sJ n# t          t          f$ r Y dS w xY w|dz  dk    r|dk    S |                     |          \  }}||k    S )Nr'   Fr   )r   rb   AssertionErrorrX   )r5   r   r   r   s       r   __contains__zSieve.__contains__1  s}    	q		A6MMMMN+ 	 	 	55	q5A: 	6M{{1~~1Avs    //c              #   B   K   t          d          D ]}| |         V  d S )Nr	   r   )r5   r   s     r   __iter__zSieve.__iter__<  s4      q 	 	Aq'MMMM	 	r   c                 |   t          |t                    r_|                     |j                   |j        |j        nd}|dk     rt          d          | j        |dz
  |j        dz
  |j                 S |dk     rt          d          t          |          }|                     |           | j        |dz
           S )zReturn the nth prime numberNr   r	   zSieve indices start at 1.)	
isinstanceslicerU   stopstart
IndexErrorr7   stepr   )r5   r   rm   s      r   __getitem__zSieve.__getitem__@  s    a 	%af%%%  !w9AGGEqy > !!<===:eai
169::1u > !!<===q		Aa   :a!e$$r   )NNNr0   )__name__
__module____qualname____doc__r<   rB   rF   rM   rU   rN   r]   r`   rX   rf   rh   rp   rE   r   r   r$   r$   &   s         "V V VC C C
0 
0 
0 
0#> #> #>J3 3 36* * * *X! ! !F* * *X  :	 	 	  % % % % %r   r$   c           	         t          |           }|dk     rt          d          |t          t          j                  k    rt          |         S ddlm} ddlm} d}t          | ||           | ||                    z   z            }||k     r%||z   dz	  } ||          |k    r|}n|dz   }||k     %t          |dz
            }||k     rt          |          r|dz  }|dz  }||k     |dz
  S )aK   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 $\le 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
    r	   z-nth must be a positive integer; prime(1) == 2r   loglir'   )r   rb   r1   siever7   &sympy.functions.elementary.exponentialrw   'sympy.functions.special.error_functionsry   rL   primepir
   )nthr   rw   ry   r   r   midn_primess           r   r>   r>   Y  sE   b 	sA1u JHIIIC Qx::::::::::::	AAss1vvCCFF#$%%A
a% 1ul2c77Q; 	AAaA a%  q1u~~H
Q, 1:: 	MH	Q Q,  q5Lr   c                   (    e Zd ZdZed             ZdS )r}   aw	   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 \times a$ be such a number, where $2 \le a \le i / j$
        Now, after sieving from primes $\le j - 1$,
        a must remain
        (because x, and hence a has no prime factor $\le j - 1$)
        Clearly, there are phi(i / j, j - 1) such a
        which remain on sieving from primes $\le j - 1$

        Now, if a is a prime less than equal to j - 1,
        $x= j \times a$ has smallest prime factor = a, and
        has already been removed(by sieving from a).
        So, we do not 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))

        $\Rightarrow$ 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, prime, prevprime, isprime
        >>> primepi(25)
        9

        So there are 9 primes less than or equal to 25. Is 25 prime?

        >>> isprime(25)
        False

        It is not. So the first prime less than 25 must be the
        9th prime:

        >>> prevprime(25) == prime(9)
        True

        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           	         |t           j        u rt           j        S |t           j        u rt           j        S 	 t	          |          }n9# t
          $ r, |j        dk    s|t           j        u rt          d          Y d S w xY w|dk     rt           j        S |t          j
        d         k    r-t          t                              |          d                   S t	          |dz            }|dz  }t          |d          }||z  |k    r|dz  }||z  |k    |dz  }dg|dz   z  }dg|dz   z  }t          d|dz             D ]}|dz
  ||<   ||z  dz
  ||<   t          d|dz             D ]}||         ||dz
           k    r||dz
           }t          dt          |||z  z  |          dz             D ]C}||z  }||k    r||xx         ||         |z
  z  cc<   '||xx         |||z           |z
  z  cc<   Dt          |||z  dz
            }	t          ||	d          D ]}||xx         |||z           |z
  z  cc<   t          |d                   S )NFzn must be realr'   r.   r   rH   r	   )r   InfinityNegativeInfinityZerorL   	TypeErroris_realNaNrb   rz   r7   rX   rW   r   min)
clsr   limarr1arr2r4   rR   r\   stlim2s
             r   evalzprimepi.eval  s   
? 	:"" 	6M	AAA 	 	 	yE! 3Q!%Z 3 !1222FF	
 q5 	6MB 	)U\\!__Q'(((!s(mmq#qkkCi1n 	1HC Ci1n 	qscAgscAgq#'"" 	! 	!A!eDG1fqjDGGq#'"" 	, 	,A Aw$q1u+% QUA1c!A,44q899 1 1U9 1GGGtBx!|+GGGGGGGtAG}q00GGGGsAEAI&&D3b)) , ,Q4Q<!++,azzs   A 2A<;A<N)rq   rr   rs   rt   classmethodr   rE   r   r   r}   r}     s<        L LZ + + [+ + +r   r}   c                 x   t          |           } t          |          }|dk    r#| }d}	 t          |          }|dz  }||k    rn|S | dk     rdS | dk     rdddddd|          S | t          j        d         k    r@t                              |           \  }}||k    rt          |dz            S t          |         S d| dz  z  }|| k    r| dz  } t          |           r| S | d	z  } n*| |z
  dk    r| dz  } t          |           r| S | d	z  } n|dz   } 	 t          |           r| S | dz  } t          |           r| S | d	z  } -)
aB   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

    r	   r'   r*   r(   r)   )r'   r(   r-   r)   r&   r?   r&   r-   )rL   r   	nextprimerz   r7   rX   r
   )r   ithr4   prr\   r   unns           r   r   r      s   0 	AAsA1u 	2BFA1u 		
 	1u q1u 1qQ1--a00EKO ||A16 	Q<8O	
AqDB	Qw 	Q1:: 	H	Q	
R1 	Q1:: 	H	QF1:: 	H	Q1:: 	H	Qr   c                    t          |           } | dk     rt          d          | dk     rdddddd|          S | t          j        d         k    r@t                              |           \  }}||k    rt          |dz
           S t          |         S d	| d	z  z  }| |z
  dk    r|dz
  } t          |           r| S | d
z  } n|dz   } 	 t          |           r| S | dz  } t          |           r| S | d
z  } -)a   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
    r(   zno preceding primes   r'   r)   )r(   r-   r)   r&   r*   r.   r	   r&   r-   )r"   rb   rz   r7   rX   r
   )r   r   r   r   s       r   	prevprimer   d  s)   & 	A1u 0.///1u 1qQ1--a00EKO ||A16 	1:8O	
AqDB2v{ F1:: 	H	QF1:: 	H	Q1:: 	H	Qr   c              #     K   |d| }} | |k    rdS |t           j        d         k    r#t                               | |          E d{V  dS t          |           dz
  } t          |          }	 t	          |           } | |k     r| V  ndS )a
   Generate a list of all prime numbers in the range [2, a),
        or [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, prime

        All primes less than 19:

        >>> list(primerange(19))
        [2, 3, 5, 7, 11, 13, 17]

        All primes greater than or equal to 7 and less than 19:

        >>> list(primerange(7, 19))
        [7, 11, 13, 17]

        All primes through the 10th prime

        >>> list(primerange(prime(10) + 1))
        [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:

        >>> from sympy import sieve
        >>> 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
        ========

        prime : Return the nth prime
        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
    Nr'   r.   r	   )rz   r7   rN   r"   r   r   s     r   rN   rN     s      V 	 !1Av EKO ##Aq))))))))) 	QAAaLLq5 	GGGGFr   c                     | |k    rdS t          t          | |f          \  } }t          j        | dz
  |          }t	          |          }||k    rt          |          }|| k     rt          d          |S )a$   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

    Nr	   z&no primes exist in the specified range)maprL   randomrandintr   r   rb   )r   r   r   rR   s       r   	randprimer     s    4 	Av sQFDAqq1ua  A!AAv aLL1u CABBBHr   Tc                    |rt          |           } nt          |           } | dk     rt          d          d}|r)t          d| dz             D ]}|t	          |          z  }nt          d| dz             D ]}||z  }|S )a:  
    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, 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

    r	   zprimorial argument must be >= 1r'   )r   rL   rb   r   r>   rN   )r   r~   rR   r4   s       r   	primorialr     s    d  1IIFF1u <:;;;	A
 q!a% 	 	AqMAA	 Aq1u%% 	 	AFAAHr   Fc              #     K   t          |pd          }dx}}| | |          }}d}||k    r@|r||k     r8|dz  }||k    r	|}|dz  }d}|r|V   | |          }|dz  }||k    r|2||k     8|r||k    r|rdS |dfV  dS |sYd}	|x}}t          |          D ]} | |          }||k    r! | |          } | |          }|	dz  }	||k    !|	r|	dz  }	||	fV  dS dS )a  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.
    r   r	   r'   N)rL   r   )
fx0nmaxvaluespowerlamtortoiseharer4   mus
             r   cycle_lengthr   Z  s     h tyq>>D OEC2dH	A
d
 	D 	AH 		QC< 	HQJEC 	JJJqwwq d
 	D 	AH 	  T	  	F*F 4s 	 	A1T77DD$ 	q{{H1T77D!GB $ 	  	!GB2g r   c           	         t          |           }|dk     rt          d          g d}|dk    r||dz
           S dt          j        d         }}||t	          |          z
  dz
  k    rN||dz
  k     r/||z   dz	  }|t	          |          z
  dz
  |k    r|}n|}||dz
  k     /t          |          r|dz  }|S ddlm} dd	lm	} d}t          | ||           | ||                    z   z            }||k     r+||z   dz	  }| ||          z
  dz
  |k    r|}n|dz   }||k     +|t	          |          z
  dz
  }||k    rt          |          s|dz  }|dz  }||k    t          |          r|dz  }|S )
a   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
    r	   z1nth must be a positive integer; composite(1) == 4)
r-   r&   r   	   
                  r   r-   r.   r   rv   rx   )r   rb   rz   r7   r}   r
   r{   rw   r|   ry   rL   )	r~   r   composite_arrr   r   r   rw   ry   n_compositess	            r   	compositer     s   0 	sA1u NLMMM888MBw $QU##ek"oqAA

NQ 	!a%i 	q5Q,CWS\\!A%)  !a%i 	 1:: 	FA::::::::::::	AAss1vvCCFF#$%%A
a% 1ulC=1q  	AAaA a%  wqzz>A%L

 qzz 	AL	Q 
  qzz 	QHr   c                 Z    t          |           } | dk     rdS | t          |           z
  dz
  S )ak   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
    r-   r   r	   )rL   r}   r   s    r   compositepir     s5    , 	AA1u qwqzz>Ar   )r	   r0   )T)NF)rt   r   r   	itertoolsr   r   r   sympy.core.functionr   sympy.core.singletonr   	primetestr
   sympy.utilities.miscr   r   r   r   r"   r$   rz   r>   r}   r   r   rN   r   r   r   r   r   rE   r   r   <module>r      s   
              " ! ! ! ! ! ( ( ( ( ( ( " " " " " "       ' ' ' ' ' '    $ $ $  m% m% m% m% m% m% m% m%`	 	G G GTz z z z zh z z zzA A A AH, , ,^\ \ \ \~# # #L? ? ? ?DV V V Vr> > >B    r   