Python同情 | sieve.primerange() 方法
借助sympy.sieve.primerange()方法,我们可以生成给定范围[a, b)的所有素数。它返回一个类型生成器对象,可以将其转换为列表以进行进一步操作。
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.
示例 #1:
# import sympy
from sympy import sieve
# Use 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))
输出:
Prime numbers for the range of numbers [1, 10) : [2, 3, 5, 7]
示例 #2:
# import sympy
from sympy import sieve
# Use sieve.primerange() method
prime_gen = sieve.primerange(8, 50)
prime_list = list(prime_gen)
print("Prime numbers for the range of numbers [8, 50) : {}".format(prime_list))
输出:
Prime numbers for the range of numbers [8, 50) : [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]