What is the sympy.primefactors() method in Python?

What is sympy library?

At its core, sympy is a library used for symbolic mathematics.

We use pip to install sympy. Refer to the following command:

pip install sympy

The primefactors method

The primefactors method is used to return a sorted list of prime factors of a given number.

Note: Prime factors are prime numbers that completely divide the given number to get zero as the remainder.

Syntax

sympy.primefactors(n)

Parameters

  • n: This is the number for which prime factors need to be obtained.

Return value

The method returns the prime factors of the given number.

Code example

import sympy
n = 100
print("The prime factors of %s is %s" % (n, sympy.primefactors(n)))

Code explanation

  • Line 1: We import the sympy package.
  • Line 3: We define n.
  • Line 5: We use the sympy.primefactors() method to obtain the prime factors of n.

Free Resources