I've following code that I need to convert to list-comprehension
(one liner). However, I am unable to do so.
The code calculates prime numbers up-to input range of A
.
def sieve(A):
l = []
f = lambda x : int(x**0.5)
for p in range(2,A+1):
for i in range(2, f(p) + 1):
if p % i == 0:
break
else:
l.append(p)
return l
So far I've following which does not work. Especially the break
within for-loop
is throwing me off.
list(set([val for sublist in [[p for i in range(2, f(p) + 1) if p %i != 0 ] for p in range(2,A) ] for val in sublist]))
EDIT
Adding constraints for the problem.
The code can only be one statement, without eval
or exec
. The code must be at most 160 character long.