Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24375

Pythonicity: Primes

$
0
0

An old interview challenge is to generate prime numbers or check if a number is prime. No advanced mathematics needed, just variants on the Sieve of Eratosthenes. Starting with a basic prime checker.

In [1]:
defisprime(n):divs=range(2,int(n**0.5)+1)returnall(n%dfordindivs)%timeisprime(1_000_003)
CPU times: user 83 µs, sys: 1e+03 ns, total: 84 µs
Wall time: 85.1 µs
Out[1]:
True

A common optimization is to skip even numbers.

In [2]:
defisprime(n):divs=range(3,int(n**0.5)+1,2)returnn==2orall(n%dfordindivs)%timeisprime(1_000_003)
CPU times: user 43 µs, sys: 0 ns, total: 43 µs
Wall time: 46.3 µs
Out[2]:
True

Brief digression on that optimization. There's nothing special about removing multiples of 2; removing multiples is the whole point. The step scalar could instead be thought of as a cycle: itertools.accumulate(itertools.repeat(2)). So removing multiples of 3 would remove every third step: itertools.accumulate(itertools.cycle([2, 4])).

Or the equivalent could be done with slicing.

In [3]:
importitertoolsdefisprime(n):divs=range(5,int(n**0.5)+1,2)returnnin(2,3)orall(n%dfordinitertools.chain(divs[::3],divs[1::3]))%timeisprime(1_000_003)
CPU times: user 42 µs, sys: 1 µs, total: 43 µs
Wall time: 44.1 µs
Out[3]:
True

The catch is the cycles grow exponentially with diminishing returns on each successive number.

Onto prime generation, while keeping the odds-only optimization. Typically it's requested to generate the first N primes, or up to some value. But that's easily generalized with itertools.islice and itertools.takewhile. A more Pythonic approach is an unbounded generator.

In [4]:
defprimes():yield2ints=itertools.count(3,2)whileTrue:prime=next(ints)yieldprimeints=(nforninintsifn%prime)list(itertools.islice(primes(),10))
Out[4]:
[2, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Elegant, but doesn't work. The problem is the scoping of prime, which is being used in the generator expression but also modified in the loop. Instead it can be replaced with a filter on a partially bound function, but unfortunately functools.partial only binds left arguments and rmod is needed here. One alternative is to use bound methods as a first-class function, even dunder methods.

In [5]:
defprimes():yield2ints=itertools.count(3,2)whileTrue:prime=next(ints)yieldprimeints=filter(prime.__rmod__,ints)%timenext(itertools.islice(primes(),1000,None))
CPU times: user 30.7 ms, sys: 1.82 ms, total: 32.5 ms
Wall time: 32 ms
Out[5]:
7927

Elegant, but slow and could overflow the stack. A more traditional approach would use the same checking logic as isprime, but also cache the primes so as to not duplicate divisors.

In [6]:
defprimes():yield2primes=[]forninitertools.count(3,2):ifall(n%pforpinitertools.takewhile(int(n**0.5).__ge__,primes)):primes.append(n)yieldn%timenext(itertools.islice(primes(),1000,None))
CPU times: user 5.49 ms, sys: 423 µs, total: 5.92 ms
Wall time: 5.8 ms
Out[6]:
7927

Onto interface design. The primes are being stored anyway, so it would be nice if they were re-iterable. A generator can be written as a class with __iter__ and __next__, but an under-appreciated feature is that __iter__ itself can be a generator. And now that it's a class, isprime can be expressed as in while also benefiting from the cache.

In [7]:
classPrimes:def__init__(self):self.ints=itertools.count(3,2)self.cache=[2]def__iter__(self):yield fromself.cacheforninself.ints:ifninself:self.cache.append(n)yieldndef__contains__(self,n):returnall(n%pforpinitertools.takewhile(int(n**0.5).__ge__,self))primes=Primes()%timenext(itertools.islice(primes,1000,None))
CPU times: user 7.89 ms, sys: 483 µs, total: 8.37 ms
Wall time: 8 ms
Out[7]:
7927
In [8]:
%time1_000_003inprimes
CPU times: user 34 µs, sys: 0 ns, total: 34 µs
Wall time: 37 µs
Out[8]:
True

There's a hybrid approach though, that's faster and nearly as simple as the above sieves. Instead of doing repeated divisions, keep track of each found prime along with the next multiple that it would eliminate. The inner loop is then optimized because it only needs to account for collisions.

In [9]:
defprimes():multiples={}forninitertools.count(2):prime=multiples.pop(n,0)ifnotprime:prime=nyieldnkey=n+primewhilekeyinmultiples:key+=primemultiples[key]=prime%timenext(itertools.islice(primes(),1000,None))
CPU times: user 2.59 ms, sys: 103 µs, total: 2.69 ms
Wall time: 2.7 ms
Out[9]:
7927

Now to add back the odds-only optimization, the step scalar needs to be double the prime number. Another way to reduce collisions is to recognize that each new prime is irrelevant until its square value is reached.

In [10]:
defprimes():yield2multiples={}forninitertools.count(3,2):step=multiples.pop(n,0)ifstep:# compositekey=n+stepwhilekeyinmultiples:key+=stepmultiples[key]=stepelse:# primemultiples[n**2]=n*2yieldn%timenext(itertools.islice(primes(),1000,None))
CPU times: user 1.37 ms, sys: 5 µs, total: 1.38 ms
Wall time: 1.38 ms
Out[10]:
7927

And finally let's add back the caching. Yielding a clean interface, an efficient implementation for all use cases, and still relatively simple.

In [11]:
classPrimes:def__init__(self):self.ints=itertools.count(3,2)self.cache=[2]self.multiples={}def__iter__(self):yield fromself.cacheforninself.ints:step=self.multiples.pop(n,0)ifstep:# compositekey=n+stepwhilekeyinself.multiples:key+=stepself.multiples[key]=stepelse:# primeself.multiples[n**2]=n*2self.cache.append(n)yieldndef__contains__(self,n):returnall(n%pforpinitertools.takewhile(int(n**0.5).__ge__,self))primes=Primes()%time1_000_003inprimes
CPU times: user 242 µs, sys: 0 ns, total: 242 µs
Wall time: 245 µs
Out[11]:
True
In [12]:
%time1_000_003inprimes
CPU times: user 40 µs, sys: 0 ns, total: 40 µs
Wall time: 43.2 µs
Out[12]:
True

Viewing all articles
Browse latest Browse all 24375

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>