Little python background first

1. [ ] represents an array.

2. range(1,5) gives the array [1, 2, 3, 4]

3. True/False – boolean values,

4. x in range(1,10) checks presence of x in the array[1, 2, …9]

5. [i for i in range(100) if i%2==0] gives an array of even numbers(watch out the enclosing square brackets, it means the result is put into an array)

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

So can you now work out what this one liner does?

[i for i in range(2,20) if True not in [True for j in range(2,i) if i%j == 0]]

P.S I think most of you will find what this does, can you think of better ways of doing it in one line?

### Like this:

Like Loading...

*Related*

Immediate thoughts. This works but you see though this is a one liner I am not sure about it’s time complexity. The best way to do prime numbers is Eratosthenes’ sieve. That is eliminate all multiples from the range of numbers you check. For eg. you check if 4 is divisible by any number less than 4. Whereas you should have eliminated 4 as a multiple of 2 in the first place.

I’ll see if i can do a “one-liner”

that is like domino effect right, u get a prime, knock off all its multiples, meanwhile take next standing number and go on.. rt?

i ll try…

yeah… but i have no clue what domino effect is…so go on with it mate.btw i forgot to think abt the one liner .. i am leaving it to u 🙂