Open In App

Python | sympy.divisors() method

Last Updated : 05 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
With the help of sympy.divisors() method, we can find all the divisors of a given number in sorted order by default.
Syntax: divisors(n, generator=False) Parameter: n - It denotes an integer. generator - If generator is True an unordered generator object is returned, otherwise it returns a sorted list of divisors. It is False by default. Returns: Returns a list of all the divisors of the given integer.
Example #1: Python3
# 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))
Output:
The divisors of 84 : [1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84]
Example #2: Python3
# 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))
Output:
The divisors of -210 : [1, 2, 3, 6, 5, 10, 15, 30, 7, 14, 21, 42, 35, 70, 105, 210]

Next Article
Article Tags :
Practice Tags :

Similar Reads