Created
December 16, 2021 04:43
-
-
Save igorvanloo/ab43929880c370e576cd5f5d3a256cfb to your computer and use it in GitHub Desktop.
Euler Totient Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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