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 sympyat 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 sympyn = 100print("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
sympymodule so we can access its methods. - Line 3: We define and initialize
n, the variable we will pass toprimenu(). - Line 5: We print the prime factors of
nusing the primefactors method. - Line 6: We print the number of distinct prime factors of
nobtained using theprimenu()method.