Welcome, guest | Sign In | My Account | Store | Cart

A one-liner that returns the list of prime numbers up to n. The inner lambda is the sieve.

Python, 4 lines
1
2
3
4
primes = lambda n: filter(lambda x, m=set(): not (x in m or m.update(range(x,n,x))), range(2,n))

>>> primes(13)
[2, 3, 5, 7, 11]
Created by Maxim Krikun on Mon, 10 Oct 2011 (MIT)
Python recipes (4591)
Maxim Krikun's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks