+
    åûj`‚  ã                   ó
  € R t ^ RIHtHt ^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
 ^RIHt ^ RIHt ^ RIHt R	 t ! R
 R4      t]! 4       tR t]! RRRR7      R 4       tR R ltRR ltR tRR ltR tRR ltR R ltR tR tR# )!z"
Generating and counting primes.

)ÚbisectÚbisect_left©Úcount)Úarray)Úrandint)Úsqrt)Úisprime)Ú
deprecated)Úas_intc                ó0   € ^ RI Hp \        V! V 4      4      # )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.)Úceiling)Ú#sympy.functions.elementary.integersr   r   )Úar   s   & ÚRC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\sympy\ntheory\generate.pyÚ_as_int_ceilingr      s   € õ <Ü‘'˜!“*ÓÐó    c                   ó~   a € ] tR t^t o RtRR ltR tRR ltR tR t	R t
RR	 ltR
 tR tR tR tR tR tRtV tR# )ÚSieveaÍ  A 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. Implementation details limit the number of
primes to ``2^32-1``.

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                óÂ  a € ^S n         \        R. RO4      S n        \        R. R	O4      S n        \        R. R
O4      S n        V^ 8:  d   \        R4      hVS n        \        ;QJ d@    V 3R lS P                  S P                  S P                  3 4       F  '       d   K   RM5	  RM1! V 3R lS P                  S P                  S P                  3 4       4      '       g   Q hR# )z¹Initial parameters for the Sieve class.

Parameters
==========

sieve_interval (int): Amount of memory to be used

Raises
======

ValueError
    If ``sieve_interval`` is not positive.

ÚLÚiz+sieve_interval should be a positive integerc              3   óT   <"  € T F  p\        V4      SP                  8H  x € K  	  R # 5i©N)ÚlenÚ_n)Ú.0r   Úselfs   & €r   Ú	<genexpr>Ú!Sieve.__init__.<locals>.<genexpr>C   s    øé € ÐUÑ.T¨”3�q“6˜TŸW™WÖ$Ó.Tùs   ƒ%(FTN)é   é   é   é   é   é   )é    é   r'   r    r    é   )r&   r'   éÿÿÿÿr)   r&   r)   )r   Ú_arrayÚ_listÚ_tlistÚ_mlistÚ
ValueErrorÚsieve_intervalÚall)r   r/   s   f&r   Ú__init__ÚSieve.__init__-   s¢   ø€ ð ˆŒÜ˜CÒ!5Ó6ˆŒ
Ü˜SÒ"4Ó5ˆŒÜ˜SÒ"7Ó8ˆŒØ˜QÔÜÐJÓKÐKØ,ˆÔß‹sÔU¨t¯z©z¸4¿;¹;ÈÏÉÑ.TÓU�s�sŠsÔU¨t¯z©z¸4¿;¹;ÈÏÉÑ.TÓU×UÒUÐUÒUr   c                ó®  € R R\        V P                  4      V P                  ^ ,          V P                  ^,          V P                  ^,          V P                  R,          V P                  R,          R\        V P                  4      V P                  ^ ,          V P                  ^,          V P                  ^,          V P                  R,          V P                  R,          R\        V P                  4      V P                  ^ ,          V P                  ^,          V P                  ^,          V P                  R,          V P                  R,          3,          # )zs<%s sieve (%i): %i, %i, %i, ... %i, %i
%s sieve (%i): %i, %i, %i, ... %i, %i
%s sieve (%i): %i, %i, %i, ... %i, %i>ÚprimeÚtotientÚmobiuséþÿÿÿr)   )r   r+   r,   r-   )r   s   &r   Ú__repr__ÚSieve.__repr__E   sà   € ð6ð ”c˜$Ÿ*™*“oØ—‘˜A• §
¡
¨1¥¨t¯z©z¸!­}Ø—‘˜B• §¡¨B¥Øœ˜DŸK™KÓ(Ø—‘˜Q• §¡¨Q¥Ø—‘˜Q• §¡¨R¥°$·+±+¸bµ/Ø”s˜4Ÿ;™;Ó'Ø—‘˜Q• §¡¨Q¥Ø—‘˜Q• §¡¨R¥°$·+±+¸bµ/ð	:CõCð 	Cr   Nc                ót  € \         ;QJ d    R WV3 4       F  '       d   K   RM	  RM! R WV3 4       4      '       d   R;p;r#V'       d   V P                  RV P                   V n        V'       d   V P                  RV P                   V n        V'       d!   V P                  RV P                   V n        R# R# )zQReset all caches (default). To reset one or more set the
desired keyword to True.c              3   ó(   "  € T F  qR J x € K
  	  R # 5ir   © )r   r   s   & r   r   ÚSieve._reset.<locals>.<genexpr>V   s   é € Ð;Ñ":˜Q�D�yÓ":ùs   ‚FTN)r0   r+   r   r,   r-   )r   r4   r5   r6   s   &&&&r   Ú_resetÚSieve._resetS   sŒ   € ÷ ‹3Ñ; 5°6Ñ":Ó;�3�3Š3Ñ; 5°6Ñ":Ó;×;Ò;Ø'+Ð+ˆEÐ+�GßØŸ™ H T§W¡WÐ-ˆDŒJßØŸ+™+ h t§w¡wÐ/ˆDŒKßØŸ+™+ h t§w¡wÐ/ˆDŽKñ r   c           
     ój  € \        V4      pV P                  R,          ^,           pW8  d   R# V^,          pW18:  d>   V ;P                  \        RV P                  W#4      4      ,          un        Y3^,          r2KC  V ;P                  \        RV P                  W!^,           4      4      ,          un        R# )z·Grow the sieve to cover all primes <= n.

Examples
========

>>> from sympy import sieve
>>> sieve._reset() # this line for doctest only
>>> sieve.extend(30)
>>> sieve[10] == 29
True
Nr   r)   )Úintr+   r*   Ú_primerange)r   ÚnÚnumÚnum2s   &&  r   ÚextendÚSieve.extend_   s„   € ô �‹Fˆð �j‰j˜�n˜qÕ ˆØŒ7ÙØ�A�vˆØŒiØ�JŠJœ&  d×&6Ñ&6°sÓ&AÓBÕB�JØ A�g’à�
Š
”f˜S $×"2Ñ"2°3¸A½Ó">Ó?Õ?�
r   c           
   #  ó.  "  € V^,          '       d
   V^,          pW8  dó   \        V P                  W!,
          ^,          4      pR.V,          pV P                  ^\        V P                  \	        V^V,          ,           ^,           4      4        F7  p\        V^,           V,           ) ^,          V,          W54       F  pRWF&   K	  	  K9  	  \        V4       F(  w  ruV'       g   K  V^V,          ,           ^,           x € K*  	  V^V,          ,          pKø  R# 5i)a¶  Generate all prime numbers in the range (a, b).

Parameters
==========

a, b : positive integers assuming the following conditions
        * a is an even number
        * 2 < self._list[-1] < a < b < nextprime(self._list[-1])**2

Yields
======

p (int): prime numbers such that ``a < p < b``

Examples
========

>>> from sympy.ntheory.generate import Sieve
>>> s = Sieve()
>>> s._list[-1]
13
>>> list(s._primerange(18, 31))
[19, 23, 29]

TFN)Úminr/   r+   r   r   ÚrangeÚ	enumerate)r   r   ÚbÚ
block_sizeÚblockÚpÚtÚidxs   &&&     r   rB   ÚSieve._primerangex   sä   é € ð4 ˆq�5Œ5Ø��FˆAØŒeÜ˜T×0Ñ0°1µ5¸Qµ,Ó?ˆJð �F˜ZÕ'ˆEØ—Z‘Z ¤&¨¯©´T¸!¸aÀ*½nÕ:LÈqÕ:PÓ5QÓ"RÓS�Ü ! a¥%¨!¥) °Õ 1°QÕ6¸
ÖF�AØ$�E“Hó Gñ Tô $ EÖ*‘�ß‘1Ø˜a #�g�+¨�/Ô)ñ +ð ��Z•ÕŠAñ ùs   ‚CDÃ$1Dc                ó¸   € \        V4      p\        V P                  4      V8  d5   V P                  \	        V P                  R,          R,          4      4       KN  R# )ax  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.
g      ø?Nr)   )r   r   r+   rF   rA   )r   r   s   &&r   Úextend_to_noÚSieve.extend_to_no¡   s@   € ô. �1‹IˆÜ�$—*‘*‹o Ô!Ø�K‰Kœ˜DŸJ™J r�N¨SÕ0Ó1Ö2ñ "r   c              #  ó&  "  € Vf   \        V4      p^pM \        ^\        V4      4      p\        V4      pW8¼  d   R# V P                  V4       V P                  \	        V P                  V4      \	        V P                  V4        Rj  x€L
  R#  L5i)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]

N)r   ÚmaxrF   r+   r   )r   r   rL   s   &&&r   Ú
primerangeÚSieve.primerange¼   sz   é € ð0 Š9Ü Ó"ˆAØ‰Aä�A” qÓ)Ó*ˆAÜ Ó"ˆAØŒ6ÙØ�‰�AŒØ—:‘:œk¨$¯*©*°aÓ8Ü)¨$¯*©*°aÓ8ð:÷ 	:ô 	:ùs   ‚BBÂBÂ	Bc           	   #  ó®  "  € \        ^\        V4      4      p\        V4      p\        V P                  4      pW8¼  d   R# W#8:  d*   \	        W4       F  pV P                  V,          x € K  	  R# V ;P                  \        R\	        W24      4      ,          un        \	        ^V4       F–  pV P                  V,          pWT^,
          8X  dh   W4,           ^,
          V,          V,          p\	        WbV4       F:  pV P                  V;;,          V P                  V,          V,          ,          uu&   K<  	  WA8¼  g   K’  Vx € K˜  	  \	        W24       Fƒ  pV P                  V,          pWT8X  dK   \	        WBV4       F:  pV P                  V;;,          V P                  V,          V,          ,          uu&   K<  	  WA8¼  g   Kn  V P                  V,          x € K…  	  R# 5i)zº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]
Nr   )rW   r   r   r,   rJ   r*   )r   r   rL   rC   r   ÚtiÚ
startindexÚjs   &&&     r   ÚtotientrangeÚSieve.totientrangeà   sM  é € ô �”? 1Ó%Ó&ˆÜ˜AÓˆÜ�—‘ÓˆØŒ6ÙØŒVÜ˜1–[�Ø—k‘k !•nÔ$ó !ð �KŠKœ6 #¤u¨Q£{Ó3Õ3�KÜ˜1˜a–[�Ø—[‘[ •^�Ø˜Q�”;Ø"#¥%¨!¥)°Õ!1°AÕ!5�JÜ" :°!Ö4˜ØŸ™ AŸ¨$¯+©+°a­.¸AÕ*=Õ=�ñ 5à–6Ø”Hñ !ô ˜1–[�Ø—[‘[ •^�Ø”7Ü" 1¨ž^˜ØŸ™ AŸ¨$¯+©+°a­.¸AÕ*=Õ=�ñ ,à–6ØŸ+™+ a�.Ô(ó !ùs   ‚D2GÄ9A<GÆ:Gc              #  ó  "  € \        ^\        V4      4      p\        V4      p\        V P                  4      pW8¼  d   R# W#8:  d*   \	        W4       F  pV P                  V,          x € K  	  R# V ;P                  \        R^ .W#,
          ,          4      ,          un        \	        ^V4       Fq  pV P                  V,          pW4,           ^,
          V,          V,          p\	        WbV4       F"  pV P                  V;;,          V,          uu&   K$  	  WA8¼  g   Km  Vx € Ks  	  \	        W24       F[  pV P                  V,          p\	        ^V,          W$4       F"  pV P                  V;;,          V,          uu&   K$  	  WA8¼  g   KW  Vx € K]  	  R# 5i)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]
Nr   )rW   r   r   r-   rJ   r*   )r   r   rL   rC   r   Úmir\   r]   s   &&&     r   ÚmobiusrangeÚSieve.mobiusrange  s%  é € ô& �”? 1Ó%Ó&ˆÜ˜AÓˆÜ�—‘ÓˆØŒ6ÙØŒVÜ˜1–[�Ø—k‘k !•nÔ$ó !ð �KŠKœ6 #¨ s¨A­E¥{Ó3Õ3�KÜ˜1˜a–[�Ø—[‘[ •^�Ø�e a�i¨AÕ-°Õ1�
Ü˜z¨aÖ0�AØ—K‘K —N bÕ(•Nñ 1à–6Ø”Hñ !ô ˜1–[�Ø—[‘[ •^�Ü˜q 1�u aÖ+�AØ—K‘K —N bÕ(•Nñ ,à–6Ø”Hó !ùs   ‚DFÄA%FÆ
Fc                ó4  € \        V4      p\        V4      pV^8  d   \        RV,          4      hWP                  R,          8”  d   V P	                  V4       \        V P                  V4      pV P                  V^,
          ,          V8X  d   W33# W3^,           3# )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)
zn should be >= 2 but got: %sr)   )r   r   r.   r+   rF   r   )r   rC   ÚtestrL   s   &&  r   ÚsearchÚSieve.search1  s   € ô" ˜qÓ!ˆÜ�1‹IˆØˆqŒ5ÜÐ;¸aÕ?Ó@Ð@Ø�z‰z˜"�~ÔØ�K‰K˜ŒNÜ�4—:‘:˜qÓ!ˆØ�:‰:�a˜!•eÕ Ô$Ø�4ˆKà˜!•e�8ˆOr   c                ó²   €  \        V4      pV^8¼  g   Q h T^,          ^ 8X  d   T^8H  # T P                  T4      w  r#Y#8H  #   \        \        3 d     R# i ; i)r    F)r   r.   ÚAssertionErrorrf   )r   rC   r   rL   s   &&  r   Ú__contains__ÚSieve.__contains__N  s`   € ð	Ü�q“	ˆAØ˜”6ˆM‘6ð ˆq�5�AŒ:Ø˜‘6ˆMØ�{‰{˜1‹~‰ˆØ‰vˆøô œNÐ+ô 	Úð	ús   ‚A ÁAÁAc              #  óF   "  € \        ^4       F  pW,          x € K  	  R# 5i)r'   Nr   )r   rC   s   & r   Ú__iter__ÚSieve.__iter__Y  s   é € Ü�q–ˆAØ•'ŒMó ùs   ‚!c                óÀ  € \        V\        4      '       d‚   V P                  VP                  4       VP                  e   VP                  M^ pV^8  d   \        R4      hV P                  V^,
          VP                  ^,
          VP                  1,          # V^8  d   \        R4      h\        V4      pV P                  V4       V P                  V^,
          ,          # )zReturn the nth prime numberzSieve indices start at 1.)	Ú
isinstanceÚslicerT   ÚstopÚstartÚ
IndexErrorr+   Ústepr   )r   rC   rs   s   && r   Ú__getitem__ÚSieve.__getitem__]  s¯   € ä�aœ×ÒØ×Ñ˜aŸf™fÔ%Ø Ÿw™wÒ2�A—G’G¸ˆEØ�qŒyô !Ð!<Ó=Ð=Ø—:‘:˜e a�i¨¯©°­
°1·6±6Ð9Õ:Ð:à�1Œuô !Ð!<Ó=Ð=Ü�q“	ˆAØ×Ñ˜aÔ Ø—:‘:˜a !�eÕ$Ð$r   )r+   r-   r   r,   r/   )i@B )NNNr   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r1   r8   r>   rF   rB   rT   rX   r^   rb   rf   rj   rm   rv   Ú__static_attributes__Ú__classdictcell__)Ú__classdict__s   @r   r   r      sW   ø‡ € ñô$Vò0Cô
0ò@ò2' òR3ô6":òH#)òJ*òXò:	ò÷%ð %r   r   c           	     ól  € \        V 4      pV^8  d   \        R4      hV\        \        P                  4      8:  d   \        V,          # ^ RIHp ^ RIHp VR8  d*   \        P                  ^V,          4       \        V,          # ^p\        W! V4      P                  4       V! V! V4      4      P                  4       ,           ,          4      pWE8  d:   WE,           ^,	          pV! V4      P                  4       V8”  d   TpK4  V^,           pK?  \        V^,
          V\        V^,
          4      ,
          4      # )a  
Return the nth prime number, where primes are indexed starting from 1:
prime(1) = 2, prime(2) = 3, etc.

Parameters
==========

nth : int
    The position of the prime number to return (must be a positive integer).

Returns
=======

int
    The nth prime number.

Examples
========

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

See Also
========

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

References
==========

.. [1] https://en.wikipedia.org/wiki/Prime_number_theorem
.. [2] https://en.wikipedia.org/wiki/Logarithmic_integral_function
.. [3] https://en.wikipedia.org/wiki/Skewes%27_number
z-nth must be a positive integer; prime(1) == 2©Úlog©Úliiè  )r   r.   r   Úsiever+   Ú&sympy.functions.elementary.exponentialr‚   Ú'sympy.functions.special.error_functionsr„   rF   rA   ÚevalfÚ	nextprimeÚ_primepi)ÚnthrC   r‚   r„   r   rL   Úmids   &      r   r4   r4   s  sç   € ôT 	ˆs‹€AØˆ1„uÜÐHÓIÐIð 	ŒC”—‘ÓÔÜ�Q�xˆå:Ý:àˆ4„xä�‰�Q˜•UÔÜ�Q�xˆà	€AäˆA��Q“—‘“¡#¡c¨!£f£+×"3Ñ"3Ó"5Õ5Õ6Ó7€Að Œ%Ø�u˜�lˆÙˆc‹7�=‰=‹?˜QÔØŠAà�a•ŠAä�Q˜•U˜A¤¨¨Q­£Õ/Ó0Ð0r   zgThe `sympy.ntheory.generate.primepi` has been moved to `sympy.functions.combinatorial.numbers.primepi`.z1.13z%deprecated-ntheory-symbolic-functions)Údeprecated_since_versionÚactive_deprecations_targetc                ó   € ^ RI Hp V! V 4      # )a»  Represents the prime counting function pi(n) = the number
of prime numbers less than or equal to n.

.. deprecated:: 1.13

    The ``primepi`` function is deprecated. Use :class:`sympy.functions.combinatorial.numbers.primepi`
    instead. See its documentation for more information. See
    :ref:`deprecated-ntheory-symbolic-functions` for details.

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
)Úprimepi)Ú%sympy.functions.combinatorial.numbersr�   )rC   Úfunc_primepis   & r   r�   r�   ¼  s   € õp NÙ˜‹?Ðr   c                ó0   € V ^8„  d   QhR\         R\         /# )r    rC   Úreturn)rA   )Úformats   "r   Ú__annotate__r–     s   € ÷ _ñ _Œsð _”sñ _r   c           	     óL  € V ^8  d   ^ # V \         P                  R,          8:  d   \         P                  V 4      ^ ,          # \        V 4      p\	        V^,           4       Uu. uF  q"^,           ^,	          NK  	  pp^ .\	        ^V^,           4       Uu. uF  q V,          ^,           ^,	          NK  	  up,           pR.V^,           ,          p\	        ^V^,           ^4       EF/  pWR,          '       d   K  W2^,
          ,          p\	        W!^,           V4       F  pRWW&   K	  	  \	        ^\        WV,          ,          V4      ^,           ^4       Fl  pWW,          '       d   K  W',          pW�8:  d$   WG;;,          WH,          V,
          ,          uu&   KD  WG;;,          W0V,          ,          V,
          ,          uu&   Kn  	  \	        V\        WV,          ^,
          4      R4       F+  pW7;;,          W7V,          ,          V,
          ,          uu&   K-  	  EK2  	  V^,          # u upi u upi )a1  Represents the prime counting function pi(n) = the number
of prime numbers less than or equal to n.

Explanation
===========

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]

Parameters
==========

n : int

FTr)   )r…   r+   rf   r   rJ   rI   )	rC   Úlimr   Úarr1Úarr2ÚskiprO   r]   Ústs	   &        r   rŠ   rŠ     s¡  € ðx 	ˆ1„uÙØŒE�K‰K˜�OÔÜ�|‰|˜A‹˜qÕ!Ð!Ü
ˆq‹'€CÜ"'¨¨a­¤.Ó1¡.˜Q��U�q�LˆL¡.€DÐ1Øˆ3¬5°°C¸!µGÔ+<Ó=Ñ+< a�a•4˜!•8 —/�/Ñ+<Ñ=Õ=€DØˆ7�c˜A•gÕ€DÜ�1�c˜A•g˜q×!ˆð �7Œ7áØ�Q•�KˆÜ�q �' 1Ö%ˆAØˆD‹Gñ &ô �qœ#˜a¨¥E�l¨CÓ0°1Õ4°aÖ8ˆAð �wŒwÙØ•ˆBØŒyØ—˜4�8 a�<Õ'•à—˜4 R¥�=¨1Õ,Õ,•ñ 9ô �sœC  q¥S¨1¥WÓ-¨rÖ2ˆAØ�G�t �F•| aÕ'Õ'�Gô 3ñ3 "ð6 ��7€Nùò= 2ùÚ=s   Á!HÂH!c                óâ  € \        V 4      p \        V4      pV^ 8:  d   \        R4      hV ^8  d   ^p V^,          pV \        P                  R,          8:  d¤   \        P                  V 4      w  r4W2,           ^,
          \        \        P                  4      8  d%   \        P                  W2,           ^,
          ,          # \        P                  R,          p W#\        \        P                  4      ,
          ,          p^V ^,          ,          pWP8X  d8   V ^,          p \        V 4      '       d   V^,          pV'       g   V # V ^,          p MMW,
          ^8X  d8   V ^,          p \        V 4      '       d   V^,          pV'       g   V # V ^,          p M	V^,           p  \        V 4      '       d   V^,          pV'       g   V # V ^,          p \        V 4      '       d   V^,          pV'       g   V # V ^,          p K]  )aˆ  Return the ith prime greater than n.

Parameters
==========

n : integer
ith : positive integer

Returns
=======

int : Return the ith prime greater than n

Raises
======

ValueError
    If ``ith <= 0``.
    If ``n`` or ``ith`` is not 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

zith should be positiver7   r)   )rA   r   r.   r…   r+   rf   r   r	   )rC   Úithr   ÚlÚ_Únns   &&    r   r‰   r‰   z  sr  € ôP 	ˆA‹€AÜˆs‹€AØˆA„vÜÐ1Ó2Ð2Øˆ1„uØˆØ	ˆQ�ˆØŒE�K‰K˜�OÔÜ�|‰|˜A‹‰ˆØ�5�1�9”sœ5Ÿ;™;Ó'Ô'Ü—;‘;˜q�u q�yÕ)Ð)Ü�K‰K˜�OˆØ	””U—[‘[Ó!Õ!Õ!ˆØ	
ˆAˆq�D�€BØ	„wØ	ˆQ�ˆÜ�1�:Š:Ø��FˆAßØ�Ø	ˆQ�‰Ø	
��1ŒØ	ˆQ�ˆÜ�1�:Š:Ø��FˆAßØ�Ø	ˆQ�‰à��FˆØ
Ü�1�:Š:Ø��FˆAßØ�Ø	ˆQ�ˆÜ�1�:Š:Ø��FˆAßØ�Ø	ˆQ�Šr   c           
     ó2  € \        V 4      p V ^8  d   \        R4      hV ^8  d   ^^^^^^^^^^/V ,          # V \        P                  R,          8:  d?   \        P	                  V 4      w  rW8X  d   \        V^,
          ,          # \        V,          # ^V ^,          ,          pW,
          ^8:  d'   V^,
          p \        V 4      '       d   V # V ^,          p M	V^,           p  \        V 4      '       d   V # V ^,          p \        V 4      '       d   V # V ^,          p K;  )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
zno preceding primesr)   )r   r.   r…   r+   rf   r	   )rC   rŸ   Úur¡   s   &   r   Ú	prevprimer¤   Í  só   € ô& 	˜Ó€AØˆ1„uÜÐ.Ó/Ð/Øˆ1„uØ�1�a˜˜A˜q ! Q¨¨1Ð-¨aÕ0Ð0ØŒE�K‰K˜�OÔÜ�|‰|˜A‹‰ˆØŒ6Ü˜˜1�•:Ðä˜•8ˆOØ	
ˆAˆq�D�€BØ…v�„{Ø��FˆÜ�1�:Š:ØˆHØ	ˆQ�‰à��FˆØ
Ü�1�:Š:ØˆHØ	ˆQ�ˆÜ�1�:Š:ØˆHØ	ˆQ�Šr   Nc              #  ó  "  € Vf   ^T rW8¼  d   R# \         P                  R,          pW8:  d    \         P                  W4       Rj  x€L
  R# W8:  d>   \         P                  \        \         P                  V 4      R  Rj  x€L
  V^,           p MV ^,          '       d
   V ^,          p \	        W^,          4      pW8  d    \         P                  W4       Rj  x€L
  Tp W8:  d   R#  \        V 4      p W8  d   V x € K  R#  Lº L� L,5i)ao  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] https://primes.utm.edu/notes/gaps.html
Nr)   )r…   r+   rX   r   rI   rB   r‰   )r   rL   Úlargest_known_primeÚtails   &&  r   rX   rX   ü  sæ   é € ðV 	‚yØ�!ˆ1Ø„vÙäŸ+™+ b�/ÐØÔÜ×#Ñ# AÓ)×)Ð)ÙàÔÜ—;‘;œ{¬5¯;©;¸Ó:Ð;Ð<×<Ð<Ø !Õ#‰Ø	
ˆQ�ŒØ	ˆQ�ˆÜˆq¨Õ*Ó+€DØ„xÜ×$Ñ$ QÓ-×-Ð-ØˆØ„vÙà
Ü�a‹LˆØŒ5ØŒGáñ) 	*ñ 	=ñ 	.ùs<   ‚ADÁDÁ:DÂDÂDÂ 9DÃDÃ)DÄDÄDc                ó¼   € W8¼  d   R# \        \        W34      w  r\        V ^,
          V4      p\        V4      pW18¼  d   \	        V4      pW08  d   \        R4      hV# )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.

Note that due to implementation difficulties,
the prime numbers chosen are not uniformly random.
For example, there are two primes in the range [112, 128),
``113`` and ``127``, but ``randprime(112, 128)`` returns ``127``
with a probability of 15/17.

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

Nz&no primes exist in the specified range)ÚmaprA   r   r‰   r¤   r.   )r   rL   rC   rO   s   &&  r   Ú	randprimerª   e  sZ   € ð@ 	„vÙÜŒs�Q�FÓ�D€AÜ��A•�qÓ€AÜ�!‹€AØ„vÜ�a‹LˆØ„uÜÐAÓBÐBØ€Hr   c                ó  € V'       d   \        V 4      p M\        V 4      p V ^8  d   \        R4      h^pV'       d/   \        ^V ^,           4       F  pV\	        V4      ,          pK  	  V# \        ^V ^,           4       F  pW#,          pK  	  V# )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

zprimorial argument must be >= 1)r   rA   r.   rJ   r4   rX   )rC   r‹   rO   r   s   &&  r   Ú	primorialr¬   ‘  s€   € ÷d Ü�1‹I‰ä�‹FˆØˆ1„uÜÐ:Ó;Ð;Ø	€Aß
Ü�q˜!˜a�%–ˆAØ”�q“�MŠAñ !ð
 €Hô ˜A˜q 1�uÖ%ˆAØ�FŠAñ &à€Hr   c              #  óæ  "  € \        T;'       g    ^ 4      p^;rEY! V4      rv^pV'       d   Vx € Wg8w  dJ   V'       d   W‚8  d<   V^,          pWE8X  d   TpV^,          p^ pV'       d   Vx € V ! V4      pV^,          pKO  V'       d   W‚8X  d   V'       d   R# VR3x € R# V'       gH   ^ p	T;rg\        V4       F  pV ! V4      pK  	  Wg8w  d   V ! V4      pV ! V4      pV	^,          p	K!  WY3x € R# R# 5i)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 gen(func, i):
    ...     while 1:
    ...         yield i
    ...         i = func(i)
    ...

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, 3)

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

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

There are 6 repeating values after the first 3.

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)
    >>> list(cycle_length(func, 4, nmax = 4, values=True))
    [4, 17, 35, 2]

Code modified from:
    https://en.wikipedia.org/wiki/Cycle_detection.
N)rA   rJ   )
ÚfÚx0ÚnmaxÚvaluesÚpowerÚlamÚtortoiseÚharer   Úmus
   &&&&      r   Úcycle_lengthr·   Ó  sí   é € ôf ˆt�yˆy�q‹>€Dð €O€EØ˜˜2›ˆdØ	€AßØŠØ
Ô
§D¨A¬HØ	ˆQ�ˆØŒ<ØˆHØ�Q�JˆEØˆCßØŠJÙ�‹wˆØˆq�Šß�”	ßÙà˜�*ÒÙßàˆØÐˆÜ�s–ˆAÙ�T“7ŠDñ àÔÙ˜“{ˆHÙ�T“7ˆDØ�!�GŠBØˆgŒñ ùs#   ‚<C1¿)C1Á)C1ÂC1ÂC1Â(A	C1c           	     óB  € \        V 4      pV^8  d   \        R4      h. ROpV^
8:  d   W!^,
          ,          # ^\        P                  R,          rCW\	        V4      ,
          ^,
          8:  d_   W4^,
          8  d6   W4,           ^,	          pV\	        V4      ,
          ^,
          V8”  d   TpK>  TpKB  \        V4      '       d
   V^,          pV# ^ RIHp ^ RIH	p ^p\        W! V4      V! V! V4      4      ,           ,          4      pW48  d9   W4,           ^,	          pWW! V4      ,
          ^,
          V8”  d   TpK3  V^,           pK>  V\	        V4      ,
          ^,
          pW�8”  d&   \        V4      '       g
   V^,          pV^,          pK+  \        V4      '       d
   V^,          pV# )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
z1nth must be a positive integer; composite(1) == 4r�   rƒ   )
r(   é   é   é	   é
   é   é   é   é   é   r)   )r   r.   r…   r+   rŠ   r	   r†   r‚   r‡   r„   rA   )	r‹   rC   Úcomposite_arrr   rL   rŒ   r‚   r„   Ún_compositess	   &        r   Ú	compositerÄ   +  sO  € ô0 	ˆs‹€AØˆ1„uÜÐLÓMÐMÚ8€MØˆB„wØ �UÕ#Ð#àŒe�k‰k˜"�o€qØ”˜“�O˜aÕÔØ�a•%ŒiØ•5˜Q•,ˆCØ”X˜c“]Õ" QÕ&¨Ô*Ø’à’Ü�1�:Š:Ø��FˆAØˆå:Ý:Ø	€AÜˆAˆs�1‹v™™C ›F›Õ#Õ$Ó%€Aà
Œ%Ø�u˜�lˆØ��C“�=˜1Õ˜qÔ ØŠAà�a•ŠAà”x “{•? QÕ&€LØ
Ô
Ü�q�zŠzØ˜AÕˆLØ	ˆQ�ŠÜˆq‡z‚zØ	ˆQ�ˆØ€Hr   c                ó\   € \        V 4      p V ^8  d   ^ # V \        V 4      ,
          ^,
          # )aî  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
)rA   rŠ   )rC   s   &r   ÚcompositepirÆ   l  s*   € ô, 	ˆA‹€AØˆ1„uÙØŒx˜‹{�?˜QÕÐr   )r'   r   )T)NF)r|   r   r   Ú	itertoolsr   r   r*   Úsympy.core.randomr   Úsympy.external.gmpyr   Ú	primetestr	   Úsympy.utilities.decoratorr
   Úsympy.utilities.miscr   r   r   r…   r4   r�   rŠ   r‰   r¤   rX   rª   r¬   r·   rÄ   rÆ   r<   r   r   Ú<module>rÍ      s«   ðñ÷
 'Ý õ "å %Ý $Ý Ý 0Ý 'ò÷T%ñ T%ñn
 	‹€òF1ñR ð kàØBôDñUó	DðUõp_ôDPòf,ô^fòR)ôX?ôDUòp>ôBr   