MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / euler_phi

Function euler_phi

maths/basic_maths.py:98–116  ·  view source on GitHub ↗

Calculate Euler's Phi Function. >>> euler_phi(100) 40 >>> euler_phi(0) Traceback (most recent call last): ... ValueError: Only positive numbers are accepted >>> euler_phi(-10) Traceback (most recent call last): ... ValueError: Only positive n

(n: int)

Source from the content-addressed store, hash-verified

96
97
98def euler_phi(n: int) -> int:
99 """Calculate Euler's Phi Function.
100 >>> euler_phi(100)
101 40
102 >>> euler_phi(0)
103 Traceback (most recent call last):
104 ...
105 ValueError: Only positive numbers are accepted
106 >>> euler_phi(-10)
107 Traceback (most recent call last):
108 ...
109 ValueError: Only positive numbers are accepted
110 """
111 if n <= 0:
112 raise ValueError("Only positive numbers are accepted")
113 s = n
114 for x in set(prime_factors(n)):
115 s *= (x - 1) / x
116 return int(s)
117
118
119if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

prime_factorsFunction · 0.70

Tested by

no test coverage detected