Prime numbers and Numpy – Python

Friday, August 27th, 2010

I've been looking at the element-wise operations in Numpy arrays. The code below generates 100K primes in about 1.3 secs on my laptop. [sourcecode language="python"] import numpy import math def prime(upto=100): return filter(lambda num: (num % numpy.arange(2,1+int(math.sqrt(num)))).all(), range(2,upto+1)) [/sourcecode] >>> prime() [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ...