Python同情 | sieve.mobiusrange() 方法
借助sympy.sieve.mobiusrange()方法,我们可以生成给定范围[a, b)的莫比乌斯数。它返回一个类型生成器对象,可以将其转换为列表以进行进一步操作。
Syntax: sieve.mobiusrange(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.mobiusrange() method
mobius_gen = sieve.mobiusrange(1, 10)
mobius_list = list(mobius_gen)
print("Mobius numbers for the range of numbers [1, 10) : {}".format(mobius_list))
输出:
Mobius numbers for the range of numbers [1, 10) : [1, -1, -1, 0, -1, 1, -1, 0, 0]
示例 #2:
# import sympy
from sympy import sieve
# Use sieve.mobiusrange() method
mobius_gen = sieve.mobiusrange(8, 20)
mobius_list = list(mobius_gen)
print("Mobius numbers for the range of numbers [8, 20) : {}".format(mobius_list))
输出:
Mobius numbers for the range of numbers [8, 20) : [0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1]