Open In App

Python | sympy.primefactors() method

Improve
Improve
Like Article
Like
Save
Share
Report

With the help of sympy.primefactors() method, we can find the prime factors of a given number. Unlike factorint(), primefactors() does not return -1 or 0.

Syntax: primefactors(n) Parameter: n – It denotes an integer. Returns: Returns a list of prime factors of the given integer.

Example #1: 

Python3




# import primefactors() method from sympy
from sympy import primefactors
 
n = 2772    # (2 * 2 * 3 * 3 * 7 * 11)
 
# Use primefactors() method
primefactors_n = primefactors(n)
 
print("The prime factors of {} : {}".format(n, primefactors_n))


Output:

The prime factors of 2772 : [2, 3, 7, 11]

Example #2: 

Python3




# import primefactors() method from sympy
from sympy import primefactors
 
n = -210 # -(2 * 3 * 5 * 7)
 
# Use primefactors() method
primefactors_n = primefactors(n)
     
print("The prime factors of {} : {}".format(n, primefactors_n))


Output:

The prime factors of -210 : [2, 3, 5, 7]

Last Updated : 30 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads