summaryrefslogtreecommitdiffstats
path: root/Doc/library/itertools.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2023-07-17 02:37:07 (GMT)
committerGitHub <noreply@github.com>2023-07-17 02:37:07 (GMT)
commitbabb22da5a25c18a2d203bf72ba35e7861ca60ee (patch)
treedae00fc6da3aa5256e43219cdb5747da8510de5a /Doc/library/itertools.rst
parent48956cc60ea05bc50b6cd73e53dd9a7d4b1dac9f (diff)
downloadcpython-babb22da5a25c18a2d203bf72ba35e7861ca60ee.zip
cpython-babb22da5a25c18a2d203bf72ba35e7861ca60ee.tar.gz
cpython-babb22da5a25c18a2d203bf72ba35e7861ca60ee.tar.bz2
Add more recipe tests. Make the factor recipe a bit faster and clearer. (GH-106817)
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r--Doc/library/itertools.rst11
1 files changed, 8 insertions, 3 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index f885254..730736b 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -1049,11 +1049,10 @@ The following recipes have a more mathematical flavor:
# factor(1_000_000_000_000_403) --> 1000000000000403
for prime in sieve(math.isqrt(n) + 1):
while True:
- quotient, remainder = divmod(n, prime)
- if remainder:
+ if n % prime:
break
yield prime
- n = quotient
+ n //= prime
if n == 1:
return
if n > 1:
@@ -1354,6 +1353,12 @@ The following recipes have a more mathematical flavor:
>>> set(sieve(10_000)).isdisjoint(carmichael)
True
+ >>> list(factor(99)) # Code example 1
+ [3, 3, 11]
+ >>> list(factor(1_000_000_000_000_007)) # Code example 2
+ [47, 59, 360620266859]
+ >>> list(factor(1_000_000_000_000_403)) # Code example 3
+ [1000000000000403]
>>> list(factor(0))
[]
>>> list(factor(1))