14

I was trying to generate all prime numbers in range x to y. I tried simple example first: range(10,11) which means to check if 10 is a prime number:
Here is my code:

prime_list = [x for x in range(10, 11) for y in range(2,x) if x % x == 0 and x % 1 == 0 and x % y != 0]

I know that the thing is missing the option to tell the expression that x%y != 0 should be checked for all y in range (2,x) and return true if and only if all have met this condition.

How do we do that?

Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64
JavaSa
  • 5,813
  • 16
  • 71
  • 121

9 Answers9

36

Use all to check all elements (from 2 upto x-1) met conditions:

>>> [x for x in range(2, 20)
     if all(x % y for y in range(2, x//2))]
[2, 3, 5, 7, 11, 13, 17, 19]
Community
  • 1
  • 1
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Could be better if it ran only upto sqrt(x). Nice use of `all`, I now know how many places I could've used it in. – bad_keypoints Jun 25 '15 at 09:08
  • 2
    You can use `range(2, int(x ** 0.5) + 1)`. (`int` is used to avoid `TypeError`) – falsetru Jun 25 '15 at 09:14
  • Nice solution! Only thing is it shouldn't check to x. I got this one. Unfortunately, it doesn't get 2 as a prime number.: [x for x in range(2,2000) if all([x % y for y in range(2,math.ceil(math.sqrt(x)) + 1)])] – peterb Sep 21 '16 at 03:02
3

One way using set comprehension can be

list(set(range(2,11)) - {x for x in range(11) for y in range(2,x) if x%y == 0})
div
  • 573
  • 5
  • 10
3

You could optimize more using the concept of the square root so as not to iterate throughout the list and calculate the prime numbers faster in the following way!!

import math
[x for x in range(2, 21) if  all(x % y != 0 for y in range(2, int(math.sqrt(x + 1)) ) )]
Cibas Mec
  • 41
  • 3
2

Program to find prime numbers within a given range using list comprehensions:

min = 10

max = 100

primes = [num for num in range(min,max) if 0 not in [num%i for i in range(2,num//2)]]

print (primes)
  • in this code it considers 4 as prime, therefore the +1 is added in the second for `primes = [num for num in range(min,max) if 0 not in [num%i for i in range(2,num//2)+1]]` – ignacio.saravia Aug 30 '23 at 21:45
1

The version with filter:

filter(lambda x:all(x % y != 0 for y in range(2, x)), range(2, 13))
NeoWang
  • 17,361
  • 24
  • 78
  • 126
1

@falsetru's answer is correct. But also, attention should be paid to optimized code. As someone said in the the comments in Kasra's answer

In [227]: %timeit [x for x in range(4, 1000) if all(x % y != 0 for y in range(2, int(m.sqrt(x)) + 1))]
100 loops, best of 3: 2.08 ms per loop

In [228]: %timeit [x for x in range(4, 1000) if all(x % y != 0 for y in range(2, int(m.sqrt(x)) + 1))]
100 loops, best of 3: 2.09 ms per loop

In [229]: %timeit [x for x in range(4, 1000) if all(x % y != 0 for y in range(2, x-1))]
100 loops, best of 3: 10.4 ms per loop

In [230]: %timeit [x for x in range(4, 1000) if all(x % y != 0 for y in range(2, x-1))]
100 loops, best of 3: 10.3 ms per loop
bad_keypoints
  • 1,382
  • 2
  • 23
  • 45
  • The code which using `sqrt` is wrong. It returns non-prime numbers. It should be something like `int(math.sqrt(x)) + 1` – falsetru Jun 25 '15 at 09:15
0

The code can be shortened to one line using lambda function:

prime = list(filter(lambda x:all(x % y !=0 for y in range(2,x)),range(2,11)))
print(prime)
Kashvi
  • 1
  • 1
0

Use this code for finding prime numbers upto given range.

>>> lower = int(input("Enter lower range: "))

>>> upper = int(input("Enter upper range: "))

>>> [x for x in range(lower, upper+1) if x>1 if all(x % y != 0 for y in range(2, x))]
cigien
  • 57,834
  • 11
  • 73
  • 112
0

Here is an example for generating prime numbers up to 1000! The same can be used for any range.

primes = [num for num in range(2,1000) if True not in [True for divisor in range(2,(int(num/2)+1)) if num % divisor == 0 and num != 2]]
Deepeshkumar
  • 395
  • 3
  • 13