Fastest-in term of space- way to find prime numbers with python

后端 未结 4 1522
温柔的废话
温柔的废话 2020-11-30 13:21

Maybe it is a stupid question, but i was wondering if you could provide the shortest source to find prime numbers with Python. I was also wondering how to find prime numbers

4条回答
  •  醉话见心
    2020-11-30 14:18

    Similar to the above, but not as cheeky as Robert King's answer:

    from itertools import ifilter, imap
    def primes(max=3000):
        r = set(); [r.add(n) for n in ifilter(lambda c: all(imap(c.__mod__, r)), xrange(2, max+1))]; return sorted(r)
    

提交回复
热议问题