8000 [3.11] Restore early-out to factor(). Strengthen tests. (GH-100591) by miss-islington · Pull Request #100592 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] Restore early-out to factor(). Strengthen tests. (GH-100591) #100592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
20 changes: 15 additions & 5 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,14 @@ which incur interpreter overhead.
"Prime factors of n."
# factor(99) --> 3 3 11
for prime in sieve(math.isqrt(n) + 1):
while n >= prime:
while True:
quotient, remainder = divmod(n, prime)
if remainder:
break
yield prime
n = quotient
if n == 1:
return
if n >= 2:
yield n

Expand Down Expand Up @@ -1256,13 +1258,21 @@ which incur interpreter overhead.
[3, 3]
>>> list(factor(10))
[2, 5]
>>> list(factor(999953*999983))
>>> list(factor(128_884_753_939)) # large prime
[128884753939]
>>> list(factor(999953 * 999983)) # large semiprime
[999953, 999983]
>>> all(math.prod(factor(n)) == n for n in range(1, 1000))
>>> list(factor(6 ** 20)) == [2] * 20 + [3] * 20 # large power
True
>>> list(factor(909_909_090_909)) # large multiterm composite
[3, 3, 7, 13, 13, 751, 113797]
>>> math.prod([3, 3, 7, 13, 13, 751, 113797])
909909090909
>>> all(math.prod(factor(n)) == n for n in range(1, 2_000))
True
>>> all(set(factor(n)) <= set(sieve(n+1)) for n in range(1, 1000))
>>> all(set(factor(n)) <= set(sieve(n+1)) for n in range(2_000))
True
>>> all(list(factor(n)) == sorted(factor(n)) for n in range(1, 1000))
>>> all(list(factor(n)) == sorted(factor(n)) for n in range(2_000))
True

>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
Expand Down
0