Skip to content

Commit 81e40f0

Browse files
gh-153222: Add math.integer.isprime() and math.integer.primes()
Draft implementation of prime-number functionality for the math.integer module (deferred out of the initial PEP 791 scope), supporting integers less than 2**64. isprime() uses the deterministic Miller-Rabin test: bases {2, 7, 61} below 4759123141, Jim Sinclair's seven bases up to 2**64 (verified against the Feitsma-Galway list of base-2 strong pseudoprimes). The result is exact for the whole supported range; larger arguments raise OverflowError. primes(start=2, stop=None) returns an iterator of the primes in [start, stop), unbounded if stop is None. Neither function is mirrored into the math namespace (PEP 791). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 3a1b547 commit 81e40f0

10 files changed

Lines changed: 821 additions & 8 deletions

Doc/library/math.integer.rst

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ These functions accept integers and objects that implement the
1313
:meth:`~object.__index__` method which is used to convert the object to an integer
1414
number.
1515

16-
The following functions are provided by this module. All return values are
17-
computed exactly and are integers.
16+
The following functions are provided by this module.
17+
Unless stated otherwise below,
18+
all return values are computed exactly and are integers.
1819

1920

2021
.. function:: comb(n, k, /)
@@ -46,6 +47,20 @@ computed exactly and are integers.
4647
returns ``0``.
4748

4849

50+
.. function:: isprime(n, /)
51+
52+
Return ``True`` if *n* is a prime number, ``False`` otherwise.
53+
54+
A prime number is a natural number greater than 1
55+
that is not a product of two smaller natural numbers.
56+
Negative numbers, ``0`` and ``1`` are not prime.
57+
58+
The argument must be less than 2\ :sup:`64`;
59+
raises :exc:`OverflowError` otherwise.
60+
61+
.. versionadded:: next
62+
63+
4964
.. function:: isqrt(n, /)
5065

5166
Return the integer square root of the nonnegative integer *n*. This is the
@@ -83,3 +98,21 @@ computed exactly and are integers.
8398
and the function returns ``n!``.
8499

85100
Raises :exc:`ValueError` if either of the arguments are negative.
101+
102+
103+
.. function:: primes(start=2, stop=None)
104+
105+
Return an iterator of the prime numbers *p* with ``start <= p < stop``,
106+
in increasing order.
107+
If *stop* is ``None`` (the default), the iteration does not stop.
108+
109+
Roughly equivalent to
110+
``(p for p in itertools.count(start) if isprime(p))``
111+
for the unbounded form.
112+
113+
The bounds must be less than 2\ :sup:`64`;
114+
raises :exc:`OverflowError` otherwise.
115+
Unbounded iteration raises :exc:`OverflowError`
116+
if the candidates reach that limit.
117+
118+
.. versionadded:: next

Doc/whatsnew/3.16.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ math
279279
(Contributed by Jeff Epler in :gh:`150534`.)
280280

281281

282+
math.integer
283+
------------
284+
285+
* Added :func:`math.integer.isprime` for primality testing of integers
286+
less than 2**64 and
287+
:func:`math.integer.primes` for iterating over prime numbers.
288+
(Contributed by Serhiy Storchaka in :gh:`153222`.)
289+
290+
282291
os
283292
--
284293

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ struct _Py_global_strings {
820820
STRUCT_FOR_ID(stdout)
821821
STRUCT_FOR_ID(step)
822822
STRUCT_FOR_ID(steps)
823+
STRUCT_FOR_ID(stop)
823824
STRUCT_FOR_ID(store_name)
824825
STRUCT_FOR_ID(strategy)
825826
STRUCT_FOR_ID(strftime)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)