add Euler's Totient Function

pull/1282/head
tianna0 2023-11-21 15:37:39 -06:00
parent d3c2184af8
commit c32fce9ffa
1 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,23 @@
# counts the positive integers up to a given integer n that are relatively prime to n
def euler_totient(n):
result = n
p = 2
while p * p <= n:
if n % p == 0:
while n % p == 0:
n //= p
result *= (1.0 - (1.0 / float(p)))
p += 1
if n > 1:
result *= (1.0 - (1.0 / float(n)))
return int(result)
# TIME COMPLEXITY - O(sqrt(N))
# Example usage
#number = 10 ———> 4