-
-

Number Theory

+
+

Number Theory

Ntheory Class Reference

-class sympy.ntheory.generate.Sieve(sieve_interval=1000000)[source]
+class sympy.ntheory.generate.Sieve(sieve_interval=1000000)[source]

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 @@ -824,7 +824,7 @@

Ntheory Class Reference
-extend(n)[source]
+extend(n)[source]

Grow the sieve to cover all primes <= n.

Examples

>>> from sympy import sieve
@@ -838,7 +838,7 @@ 

Ntheory Class Reference
-extend_to_no(i)[source]
+extend_to_no(i)[source]

Extend to include the ith prime number.

Parameters:
@@ -860,7 +860,7 @@

Ntheory Class Reference
-mobiusrange(a, b)[source]
+mobiusrange(a, b)[source]

Generate all mobius numbers for the range [a, b).

Parameters:
@@ -884,7 +884,7 @@

Ntheory Class Reference
-primerange(a, b=None)[source]
+primerange(a, b=None)[source]

Generate all prime numbers in the range [2, a) or [a, b).

Examples

>>> from sympy import sieve, prime
@@ -909,7 +909,7 @@ 

Ntheory Class Reference
-search(n)[source]
+search(n)[source]

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 @@ -926,7 +926,7 @@

Ntheory Class Reference
-totientrange(a, b)[source]
+totientrange(a, b)[source]

Generate all totient numbers for the range [a, b).

Examples

>>> from sympy import sieve
@@ -938,12 +938,65 @@ 

Ntheory Class Reference

+
+
+class sympy.ntheory.factor_.FactorCache(maxsize: int | None = None)[source]
+

Provides a cache for prime factors. +factor_cache is pre-prepared as an instance of FactorCache, +and factorint internally references it to speed up +the factorization of prime factors.

+

While cache is automatically added during the execution of factorint, +users can also manually add prime factors independently.

+
>>> from sympy import factor_cache
+>>> factor_cache[15] = 5
+
+
+

Furthermore, by customizing get_external, +it is also possible to use external databases. +The following is an example using http://factordb.com .

+
import requests
+from sympy import factor_cache
+
+def get_external(self, n: int) -> list[int] | None:
+    res = requests.get("http://factordb.com/api", params={"query": str(n)})
+    if res.status_code != requests.codes.ok:
+        return None
+    j = res.json()
+    if j.get("status") in ["FF", "P"]:
+        return list(int(p) for p, _ in j.get("factors"))
+
+factor_cache.get_external = get_external
+
+
+

Be aware that writing this code will trigger internet access +to factordb.com when calling factorint.

+
+
+cache_clear() None[source]
+

Clear the cache

+
+ +
+
+get(n: int, default=None)[source]
+

Return the prime factor of n. +If it does not exist in the cache, return the value of default.

+
+ +
+
+property maxsize: int | None
+

Returns the maximum cache size; if None, it is unlimited.

+
+ +
+

-
-

Ntheory Functions Reference

+
+

Ntheory Functions Reference

-sympy.ntheory.generate.prime(nth)[source]
+sympy.ntheory.generate.prime(nth)[source]

Return the nth prime number, where primes are indexed starting from 1: prime(1) = 2, prime(2) = 3, etc.

@@ -1000,7 +1053,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.primepi(n)[source]
+sympy.ntheory.generate.primepi(n)[source]

Represents the prime counting function pi(n) = the number of prime numbers less than or equal to n.

@@ -1076,7 +1129,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.nextprime(n, ith=1)[source]
+sympy.ntheory.generate.nextprime(n, ith=1)[source]

Return the ith prime greater than n.

Parameters:
@@ -1117,7 +1170,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.prevprime(n)[source]
+sympy.ntheory.generate.prevprime(n)[source]

Return the largest prime smaller than n.

Notes

Potential primes are located at 6*j +/- 1. This @@ -1140,7 +1193,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.primerange(a, b=None)[source]
+sympy.ntheory.generate.primerange(a, b=None)[source]

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 @@ -1237,7 +1290,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.randprime(a, b)[source]
+sympy.ntheory.generate.randprime(a, b)[source]

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.

@@ -1272,7 +1325,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.primorial(n, nth=True)[source]
+sympy.ntheory.generate.primorial(n, nth=True)[source]

Returns the product of the first n primes (default) or the primes less than or equal to n (when nth=False).

Examples

@@ -1323,7 +1376,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.cycle_length(f, x0, nmax=None, values=False)[source]
+sympy.ntheory.generate.cycle_length(f, x0, nmax=None, values=False)[source]

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 @@ -1376,7 +1429,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.composite(nth)[source]
+sympy.ntheory.generate.composite(nth)[source]

Return the nth composite number, with the composite numbers indexed as composite(1) = 4, composite(2) = 6, etc….

Examples

@@ -1408,7 +1461,7 @@

Ntheory Functions Reference
-sympy.ntheory.generate.compositepi(n)[source]
+sympy.ntheory.generate.compositepi(n)[source]

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

@@ -1438,7 +1491,7 @@

Ntheory Functions Reference
-sympy.ntheory.factor_.smoothness(n)[source]
+sympy.ntheory.factor_.smoothness(n)[source]

Return the B-smooth and B-power smooth values of n.

The smoothness of n is the largest prime factor of n; the power- smoothness is the largest divisor raised to its multiplicity.

@@ -1460,7 +1513,7 @@

Ntheory Functions Reference
-sympy.ntheory.factor_.smoothness_p(n, m=-1, power=0, visual=None)[source]
+sympy.ntheory.factor_.smoothness_p(n, m=-1, power=0, visual=None)[source]

Return a list of [m, (p, (M, sm(p + m), psm(p + m)))…] where:

    @@ -1553,7 +1606,7 @@

    Ntheory Functions Reference
    -sympy.ntheory.factor_.multiplicity(p, n)[source]
    +sympy.ntheory.factor_.multiplicity(p, n)[source]

    Find the greatest integer m such that p**m divides n.

    Examples

    >>> from sympy import multiplicity, Rational
    @@ -1594,7 +1647,7 @@ 

    Ntheory Functions Referencefactor=True,

-)[source] +)[source]

Return (b, e) such that n == b**e if n is a unique perfect power with e > 1, else False (e.g. 1 is not a perfect power). A ValueError is raised if n is not Rational.

@@ -1670,7 +1723,7 @@

Ntheory Functions ReferenceF=None,

-)[source] +)[source]

Use Pollard’s rho method to try to extract a nontrivial factor of n. The returned factor may be a composite number. If no factor is found, None is returned.

@@ -1756,7 +1809,7 @@

Ntheory Functions Reference
-sympy.ntheory.factor_.pollard_pm1(n, B=10, a=2, retries=0, seed=1234)[source]
+sympy.ntheory.factor_.pollard_pm1(n, B=10, a=2, retries=0, seed=1234)[source]

Use Pollard’s p-1 method to try to extract a nontrivial factor of n. Either a divisor (perhaps composite) or None is returned.

The value of a is the base that is used in the test gcd(a**M - 1, n). @@ -1901,7 +1954,7 @@

Ntheory Functions Referencemultiple=False,

-)[source] +)[source]

Given a positive integer n, factorint(n) returns a dict containing the prime factors of n as keys and their respective multiplicities as values. For example:

@@ -2078,7 +2131,7 @@

Ntheory Functions Referencemultiple=False,