Open In App

Python | sympy.totient() method

With the help of sympy.totient() method, we can find Euler totient function or phi(n) of a given integer. Euler totient function is the number of positive integers less than or equal to a given integer that are relatively prime to it. In other words, it is the number of integers k in the range 1 <= k <= n for which the greatest common divisor gcd(n, k) is equal to 1.

Syntax: totient(n)



Parameter:
n – It denotes an integer.

Returns: Returns the number of integers less than or equal to that integer n that are relatively prime to it.



Example #1:




# import totient() method from sympy
from sympy.ntheory.factor_ import totient
  
n = 24
  
# Use totient() method 
totient_n = totient(n) 
      
print("phi({}) =  {} ".format(n, totient_n)) # 1 5 7 11 13 17 19 23

Output:

phi(24) =  8

Example #2:




# import totient() method from sympy
from sympy.ntheory.factor_ import totient
  
n = 19
  
# Use totient() method 
totient_n = totient(n) 
      
print("phi({}) =  {} ".format(n, totient_n))

Output:

phi(19) =  18
Article Tags :