Thursday, July 4, 2024
HomeLanguagesPythonPython | sympy.primefactors() method

Python | sympy.primefactors() method

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]
Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments