mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
[3.13] Simplify and speed-up an itertools recipe (gh-127848) (gh-127998)
This commit is contained in:
parent
231c93923d
commit
28e684b330
1 changed files with 6 additions and 6 deletions
|
@ -1015,7 +1015,7 @@ The following recipes have a more mathematical flavor:
|
||||||
.. testcode::
|
.. testcode::
|
||||||
|
|
||||||
def powerset(iterable):
|
def powerset(iterable):
|
||||||
"powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
# powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
|
||||||
s = list(iterable)
|
s = list(iterable)
|
||||||
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
||||||
|
|
||||||
|
@ -1104,11 +1104,6 @@ The following recipes have a more mathematical flavor:
|
||||||
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
|
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
|
||||||
yield from iter_index(data, 1, start=3)
|
yield from iter_index(data, 1, start=3)
|
||||||
|
|
||||||
def is_prime(n):
|
|
||||||
"Return True if n is prime."
|
|
||||||
# is_prime(1_000_000_000_000_403) → True
|
|
||||||
return n > 1 and all(n % p for p in sieve(math.isqrt(n) + 1))
|
|
||||||
|
|
||||||
def factor(n):
|
def factor(n):
|
||||||
"Prime factors of n."
|
"Prime factors of n."
|
||||||
# factor(99) → 3 3 11
|
# factor(99) → 3 3 11
|
||||||
|
@ -1123,6 +1118,11 @@ The following recipes have a more mathematical flavor:
|
||||||
if n > 1:
|
if n > 1:
|
||||||
yield n
|
yield n
|
||||||
|
|
||||||
|
def is_prime(n):
|
||||||
|
"Return True if n is prime."
|
||||||
|
# is_prime(1_000_000_000_000_403) → True
|
||||||
|
return n > 1 and next(factor(n)) == n
|
||||||
|
|
||||||
def totient(n):
|
def totient(n):
|
||||||
"Count of natural numbers up to n that are coprime to n."
|
"Count of natural numbers up to n that are coprime to n."
|
||||||
# https://mathworld.wolfram.com/TotientFunction.html
|
# https://mathworld.wolfram.com/TotientFunction.html
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue