Python sympy | sieve.primerange () method

Using the sympy.sieve.primerange () method, we can generate all primes for a given [a, b) range. It returns a type generator object that can be converted to a list for further operations.

Syntax: sieve.primerange (a, b)
Parameters :
a - It denotes the start of the range. It is inclusive.
b - It denotes the end of the range. It is not inclusive.
Returns: The method returns a type generator object.

Example # 1:

# import sympy

from sympy import sieve

 
# Use the sieve.primerange () method

prime_gen = sieve.primerange ( 1 , 10

prime_list = list (prime_gen)

  

print ( "Prime numbers for the range of numbers [1, 10): { } " . format (prime_list)) 

Exit:

 Prime numbers for the range of numbers [1, 10): [2, 3, 5, 7] 

Example # 2:

# import sympy

from sympy import sieve

 
# Use the sieve.primerange () method

prime_gen = sieve.primerange ( 8 < code class = "plain">, 50

prime_list = list (prime_gen)

 

print ( "Prime numbers for the range of numbers [8, 50): {}" . format (prime_list)) 

Exit :

 Prime numbers for the range of numbers [8, 50): [11, 13, 17, 19, 23, 29, 31 , 37, 41, 43, 47] 




Get Solution for free from DataCamp guru