K i`dZddlmZmZddlmZddlmZddlmZddl m Z ddl m Z dd l mZdd lmZd ZGd d ZeZdZeddddZdedefdZd dZdZd!dZdZd"dZd#dZdZdZy)$z" Generating and counting primes. )bisect bisect_leftcount)array)randint)sqrt)isprime) deprecated)as_intc0ddlm}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.integersrr )ars \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/ntheory/generate.py_as_int_ceilingrs< '!* cdeZdZdZddZdZddZdZdZdZ dd Z d Z d Z d Z d ZdZdZy)SieveaA 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"d_tdgd_tdgd_tdgd_|dkr t d|_tfd jjjfDsJy ) 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) )rr r rri)rr r"rr"rz+sieve_interval should be a positive integerc3NK|]}t|jk(ywN)len_n).0r!selfs r z!Sieve.__init__..CsU3q6TWW$Us"%N)r&_array_list_tlist_mlist ValueErrorsieve_intervalall)r(r/s` r__init__zSieve.__init__-s|C!56 S"45 S"78 Q JK K,Utzz4;; .TUUUUrc.ddt|j|jd|jd|jd|jd|jddt|j|jd|jd|jd|jd|jdd t|j|jd|jd|jd|jd|jdfzS) 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>primerr rr"totientmobius)r%r+r,r-)r(s r__repr__zSieve.__repr__Es6c$**oA 1 tzz!}BB DKK(QQQR$++b/ s4;;'QQQR$++b/ :C C CrNctd|||fDrdx}x}}|r|jd|j|_|r|jd|j|_|r|jd|j|_yy)z]Reset all caches (default). To reset one or more set the desired keyword to True.c3$K|]}|du ywr$)r'r!s rr)zSieve._reset..Vs;QqDy;sTN)r0r+r&r,r-)r(r3r5r6s r_resetz Sieve._resetSsx ;5'6":; ;'+ +E +Gf HTWW-DJ ++htww/DK ++htww/DK rc :t|}|jddz}||kry|dz}||kr<|xjtd|j||z c_||dz}}||kr<|xjtd|j||dzz c_y)zGrow 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 r"r Nrr)intr+r* _primerange)r(nnumnum2s rextendz Sieve.extend_s Fjjnq  s7 Avai JJ&d&6&6sD&AB BJdAgCai fS$"2"23A">?? rc #K|dzr|dz}||krt|j||z dz}dg|z}|jdt|jt |d|zzdzD]&}t |dz|z dz|z||D]}d||< (t |D]\}}|s |d|zzdz|d|zz }||kryyw)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] rr TFN)minr/r+rr range enumerate)r(rb block_sizeblockptidxs rr>zSieve._primerangexs4 q5 FA!eT001q5Q,?JFZ'EZZ&T!a*n:Lq:P5Q"RS %!a%!)  1Q6 AF%A$E!H% %$E* *Qa#g+/) * Z A!esB#C&CCct|}t|j|krD|jt |jddzt|j|krCyy)aExtend 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 r%r+rBr=)r(r!s r extend_to_nozSieve.extend_to_nosM. 1I$**o! KKDJJrNS01 2$**o!rc#K|t|}d}n tdt|}t|}||k\ry|j||jt |j|t |j|Ed{y7w)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)rmaxrBr+r)r(rrGs r primerangezSieve.primeranges}0 9"AAAq)*A"A 6  A::k$**a8)$**a8: : :sBB B B c #Ktdt|}t|}t|j}||k\ry||kr#t ||D]}|j|y|xjt dt ||z c_t d|D]j}|j|}||dz k(rG||zdz |z|z}t |||D])}|j|xx|j||zzcc<+||k\sg|lt ||D]f}|j|}||k(r9t |||D])}|j|xx|j||zzcc<+||k\sV|j|hyw)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)rPrr%r,rEr*)r(rrGr?r!ti startindexjs r totientrangezSieve.totientranges ?1% & A    6  !V1a[ %kk!n$ % KK6#uQ{3 3K1a[ [[^Q;"#a%!)!1A!5J":q!4> A$++a.A*==>6H 1a[ )[[^7"1a^> A$++a.A*==>6++a.(  )sC?E>A'E>*E>c#Ktdt|}t|}t|j}||k\ry||kr#t ||D]}|j|y|xjt ddg||z zz c_t d|D]R}|j|}||zdz |z|z}t |||D]}|j|xx|zcc<||k\sO|Tt ||D]G}|j|}t d|z||D]}|j|xx|zcc<||k\sD|Iyw)aGenerate 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!rr)rPrr%r-rEr*)r(rrGr?r!mirTrUs r mobiusrangezSieve.mobiusrangesS& ?1% & A    6  !V1a[ %kk!n$ % KK6#sAE{3 3K1a[ [[^!eaiA-1 z1a0)AKKNb(N)6H  1a[ [[^q1ua+)AKKNb(N)6H  sC$E'AE=Ect|}t|}|dkrtd|z||jdkDr|j |t |j|}|j|dz |k(r||fS||dzfS)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) rzn should be >= 2 but got: %sr"r )rr r.r+rBr)r(r?testrGs rsearchz Sieve.search1s"q! 1I q5;a?@ @ tzz"~  KKN 4::q ! ::a!e  $a4Ka!e8Orc t|}|dk\sJ |dzdk(r|dk(S|j|\}}||k(S#ttf$rYywxYw)NrFr)r r.AssertionErrorr\)r(r?rrGs r __contains__zSieve.__contains__Nsd q A6M6 q5A:6M{{1~1Av N+  s;A  A c#:KtdD] }|| yw)Nr r)r(r?s r__iter__zSieve.__iter__Ys"q Aq'M sct|trq|j|j|j |jnd}|dkr t d|j |dz |jdz |jS|dkr t dt|}|j||j |dz S)zReturn the nth prime numberrr zSieve indices start at 1.) isinstanceslicerNstopstart IndexErrorr+stepr )r(r?rfs r __getitem__zSieve.__getitem__]s a    aff % ww2AGGEqy!!<==::eai 1669: :1u!!<==q A   a ::a!e$ $r)i@B)NNNr$)__name__ __module__ __qualname____doc__r1r7r;rBr>rNrQrVrYr\r_rarir:rrrrsO$V0 C 0@2' R36":H#)J*X: %rrc  t|}|dkr td|ttjkr t|Sddlm}ddlm}|dkr!tjd|zt|Sd}t|||j|||jzz}||kr/||zdz }||j|kDr|}n|dz}||kr/t|dz |t|dz z S) 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 r z-nth must be a positive integer; prime(1) == 2rlogliir)r r.r%siever+&sympy.functions.elementary.exponentialrp'sympy.functions.special.error_functionsrrrBr=evalf nextprime_primepi)nthr?rprrrrGmids rr3r3ssT s A1uHII C Qx::4x QUQx A AQ#c!f+"3"3"55 67A a%1ul c7==?Q AaA a% QUAQ/ 00rzgThe `sympy.ntheory.generate.primepi` has been moved to `sympy.functions.combinatorial.numbers.primepi`.z1.13z%deprecated-ntheory-symbolic-functions)deprecated_since_versionactive_deprecations_targetcddlm}||S)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 r)primepi)%sympy.functions.combinatorial.numbersr)r? func_primepis rrrspN ?rr?returnc |dkry|tjdkrtj|dSt|}t |dzDcgc] }|dzdz  }}dgt d|dzDcgc] }||zdzdz c}z}dg|dzz}t d|dzdD]}||r ||dz }t ||dz|D]}d||< t dt |||zz|dzdD]<}||r ||z}||kr||xx|||z zcc<'||xx|||z|z zcc<>t |t |||zdz dD]}||xx|||z|z zcc<|dScc}wcc}w)a 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 rrr"r FrT)rtr+r\r rErD) r?limr!arr1arr2skiprJrUsts rryrysx 1uEKKO||Aq!! q'C"'a. 1QQUqL 1D 1 35C!G+<=a1a4!8/= =D 7cAg D 1cAgq !( 7  QKq#'1% ADG q#aAElC014a8 -AAwQBSyQ48a<'Q4R=1,, -sCQqS1W-r2 (A GtAF|a' 'G (3(6 7N= 2=s E-4E2ct|}t|}|dkr td|dkrd}|dz}|tjdkrtj |\}}||zdz t tjkrtj||zdz Stjd}||t tjz z }d|dzz}||k(r|dz }t|r |dz}|s|S|dz }n,||z d k(r|dz }t|r |dz}|s|S|dz }n|d z} t|r |dz}|s|S|dz }t|r |dz}|s|S|dz }3) aU 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 rzith should be positiverr r4r"rr r)r=r r.rtr+r\r%r )r?ithr!l_nns rrxrxzs{P AAs AAv1221u  QEKKO||A1 q519s5;;' ';;q1uqy) ) KKO QU[[! !! AqDB Qw Q 1: FA Q R1 Q 1: FA Q F 1: FA Q 1: FA Q rct|}|dkr td|dkr dddddd|S|tjdkr2tj |\}}||k(r t|dz St|Sd |d zz}||z dkr|dz }t |r|S|d z}n|dz} t |r|S|dz}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 rzno preceding primesrsrr)rr rrrr"r rr )rr.rtr+r\r )r?rurs r prevprimers& A1u.//1uqQ1-a00EKKO||A1 61: 8O AqDB2v{ F 1:H Q F 1:H Q 1:H Q rNc#K|d|}}||k\rytjd}||krtj||Ed{y||kr9tjttj|dEd{|dz}n |dzr|dz}t ||dz}||kr tj ||Ed{|}||kry t |}||kr|ny77h7(w)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] https://primes.utm.edu/notes/gaps.html Nrr"r )rtr+rQrrDr>rx)rrGlargest_known_primetails rrQrQsV y!1Av++b/ ##Aq))) ;;{5;;:;<<< ! # Q Q q&* +D4x$$Q--- Av aL q5G   * = .s6>C&C 8C&9C":AC&;C$<%C&"C&$C&c||k\rytt||f\}}t|dz |}t|}||k\r t |}||kr t d|S)aG 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 Nr z&no primes exist in the specified range)mapr=rrxrr.)rrGr?rJs r randprimeresd@ Av sQF DAqAqA! AAv aL1uABB Hrc|r t|}n t|}|dkr tdd}|r$td|dzD]}|t |z}|St d|dzD]}||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 zprimorial argument must be >= 1r)r r=r.rEr3rQ)r?rzrJr!s r primorialrsd 1I F1u:;; A q!a% A qMA  HAq1u% A FA  Hrc#Kt|xsd}dx}}|||}}d}|r|||k7r;|r||kr4|dz }||k(r |}|dz}d}|r|||}|dz }||k7r |s.||kr4|r||k(r |ry|dfy|sEd} |x}}t|D] }||} ||k7r||}||}| dz } ||k7r|| fyyw)awFor 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. rr rN)r=rE) fx0nmaxvaluespowerlamtortoiseharer!mus r cycle_lengthrs#f tyq>DOEC2dH A  d DAH Q C<H QJEC Jw q d DAH T  *    4s AT7D ${HT7D !GB$2g  sAC"C(AC9Cc tt|}|dkr tdgd}|dkr||dz Sdtjd}}||t |z dz krD||dz kr*||zdz }|t |z dz |kDr|}n|}||dz kr*t |r|dz}|Sddlm}dd lm }d}t||||||zz}||kr'||zdz }|||z dz |kDr|}n|dz}||kr'|t |z dz }||kDrt |s|dz}|dz}||kDrt |r|dz}|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 rrs rr r"rrorq) r r.rtr+ryr rurprvrrr=) rzr? composite_arrrrGr{rprr n_compositess r compositer+s0 s A1uLMM8MBwQU## ekk"oqAA Oa !a%iq5Q,CXc]"Q&* !a%i 1: FA:: A As1vCF # $%A a%1ul C=1 q AaA a%x{?Q&L  qz A L Q  qz Q HrcFt|}|dkry|t|z dz 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 rr )r=ry)r?s r compositepirls*, AA1u x{?Q r)r r$)T)NF) rmrr itertoolsrrr*sympy.core.randomrsympy.external.gmpyr primetestr sympy.utilities.decoratorr sympy.utilities.miscr rrrtr3rr=ryrxrrQrrrrrr:rrrs '"%$0'T%T%n F1R  kBDU DUp_s_s_DPf,^fR) X? DUp> Br