Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created December 16, 2021 04:43
Show Gist options
  • Select an option

  • Save igorvanloo/ab43929880c370e576cd5f5d3a256cfb to your computer and use it in GitHub Desktop.

Select an option

Save igorvanloo/ab43929880c370e576cd5f5d3a256cfb to your computer and use it in GitHub Desktop.
Euler Totient Function
def phi(n):
if n == 1:
return 1
phi = 1
d = 2
while n > 1:
count = 0
while n % d == 0:
count += 1
n /= d
if count > 0:
phi *= (d**(count-1))*(d-1)
d = d + 1
if d*d > n:
if n > 1:
phi *= int(n - 1)
break
return phi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment