fork download
  1. def euler_totient(n):
  2. result = n
  3. p = 2
  4. while p * p <= n:
  5. # Check if p is a prime factor
  6. if n % p == 0:
  7. # If yes, then update n and result
  8. while n % p == 0:
  9. n //= p
  10. result -= result // p
  11. p += 1
  12. # If n is a prime number greater than 2
  13. if n > 1:
  14. result -= result // n
  15. return result
  16.  
  17. # Example usage
  18. n = 10
  19. print(f"Euler's totient function of {n} is: {euler_totient(n)}")
Success #stdin #stdout 0.03s 9640KB
stdin
Standard input is empty
stdout
Euler's totient function of 10 is: 4