📜  Python| sympy.divisors() 方法

📅  最后修改于: 2022-05-13 01:54:37.350000             🧑  作者: Mango

Python| sympy.divisors() 方法

借助sympy.divisors()方法,我们可以默认按排序顺序找到给定数字的所有除数。

示例 #1:

# import divisors() method from sympy
from sympy import divisors
  
n = 84
  
# Use divisors() method 
divisors_n = divisors(n) 
      
print("The divisors of {} : {}".format(n, divisors_n))

输出:

The divisors of 84 : [1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84]

示例 #2:

# import divisors() method from sympy
from sympy import divisors
  
n = -210
  
# Use divisors() method 
divisors_n = list(divisors(n, generator = True)) 
      
print("The divisors of {} : {}".format(n, divisors_n))

输出:

The divisors of -210 : [1, 2, 3, 6, 5, 10, 15, 30, 7, 14, 21, 42, 35, 70, 105, 210]