diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2024-12-12 03:24:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-12 03:24:56 (GMT) |
commit | 8bbd379ee30db0320ec3d31c37aee2a503902b0f (patch) | |
tree | 61bbf51d3db9cff1033645af9f9e416f6ef2ba80 | |
parent | c33b6fbf358c1bc14b20e14a1fffff62c6826ecd (diff) | |
download | cpython-8bbd379ee30db0320ec3d31c37aee2a503902b0f.zip cpython-8bbd379ee30db0320ec3d31c37aee2a503902b0f.tar.gz cpython-8bbd379ee30db0320ec3d31c37aee2a503902b0f.tar.bz2 |
Simplify and speed-up an itertools recipe (gh-127848)
-rw-r--r-- | Doc/library/itertools.rst | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 03966f3..3b90d78 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -1015,7 +1015,7 @@ The following recipes have a more mathematical flavor: .. testcode:: 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) 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))) 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): "Prime factors of n." # factor(99) → 3 3 11 @@ -1123,6 +1118,11 @@ The following recipes have a more mathematical flavor: if n > 1: 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): "Count of natural numbers up to n that are coprime to n." # https://mathworld.wolfram.com/TotientFunction.html |