12.07.18 15:15, David Mertz пише:
On Thu, Jul 12, 2018, 7:56 AM Serhiy Storchaka <storchaka@gmail.com <mailto:storchaka@gmail.com>> wrote:
* isprime(n) Tests if n is a prime number.
How would you test this? The Miller-Rabin Primality test? For large numbers, iterating through candidate prime divisors is pricey.
* primes() Returns an iterator of prime numbers: 2, 3, 5, 7, 11, 13,...
How would you implements this? Sieve of Eratosthenes is really fun to show students as a Python generator function. But the cached primes DO grow unboundedly as you utilize the generator. Wheel factorization as first pass? Do you cached the first N primes so the each instance of iterator can provide initial elements much faster? What is N?
I didn't mean any concrete implementation. Sure there are enough efficient and simple. I sometimes need a sequence of prime numbers for solving toy problems, and quickly write something like: def primes(n): return (i for i in range(2, n) if all(i % j for j in primes(int(sqrt(i)) + 1))) Having such feature in the stdlib would be very convenient. Any reasonable implementation is enough efficient for my purposes.