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

The Python sympy library

Python sympy is an open-source library used for symbolic mathematics. We can use pip to install sympy by using the following command.

pip install sympy

The primenu() method

The primenu() method is used to return the number of distinct prime factors for a given number. Here is how we can use the method in code.

sympy.primenu(n)

Note: Do not forget to import sympy at the start of your Python script.

In the above example, we’ve passed n to the primenu() method. n has to be a positive integer. primenu() will return the number of distinct prime factors for the given positive integer n.

Example

Here is a simple Python script that uses primenu().

import sympy
n = 100
print("The prime factors of %s are %s" % (n, sympy.primefactors(n)))
print("The number of distinct prime factors for %s is %s" % (n, sympy.primenu(n)))

Code explanation

  • Click “Run” to see the result of the code above. Change the value of n and hit “Run” again to see the change in the result.
  • Line 1: We import the sympy module so we can access its methods.
  • Line 3: We define and initialize n, the variable we will pass to primenu().
  • Line 5: We print the prime factors of n using the primefactors() method.
  • Line 6: We print the number of distinct prime factors of n obtained using the primenu() method.