Python同情 | sieve.totientrange() 方法
在sympy.sieve.totientrange()方法的帮助下,我们可以生成给定范围[a, b)的所有 totient 数字。它返回一个类型生成器对象,可以将其转换为列表以进行进一步操作。
Syntax: sieve.totientrange(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.totientrange() method
totient_gen = sieve.totientrange(1, 10)
totient_list = list(totient_gen)
print("Totient numbers for the range of numbers [1, 10) : {}".format(totient_list))
输出:
Totient numbers for the range of numbers [1, 10) : [1, 1, 2, 2, 4, 2, 6, 4, 6]
示例 #2:
# import sympy
from sympy import sieve
# Use sieve.totientrange() method
totient_gen = sieve.totientrange(8, 20)
totient_list = list(totient_gen)
print("Totient numbers for the range of numbers [8, 20) : {}".format(totient_list))
输出:
Totient numbers for the range of numbers [8, 20) : [4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 18]