python - Finding prime numbers using list comprehention -
i trying generate prime numbers in range x y. tried simple example first: range(10,11)
means check if 10 prime number:
here code:
prime_list = [x x in range(10, 11) y in range(2,x) if x % x == 0 , x % 1 == 0 , x % y != 0]
i know thing missing option tell expression x%y != 0
should checked y in range (2,x)
, return true if , if have met condition.
how that?
use all
check elements (from 2 upto x-1) met conditions:
>>> [x x in range(2, 20) if all(x % y != 0 y in range(2, x))] [2, 3, 5, 7, 11, 13, 17, 19]
Comments
Post a Comment