forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Euler's Totient function (keon#421)
* add Euler's Totient function * add link to Euler's totient function in README
- Loading branch information
1 parent
89aabde
commit 62a3ba2
Showing
4 changed files
with
38 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Euler's totient function, also known as phi-function ϕ(n), | ||
counts the number of integers between 1 and n inclusive, | ||
which are coprime to n. | ||
(Two numbers are coprime if their greatest common divisor (GCD) equals 1). | ||
""" | ||
def euler_totient(n): | ||
"""Euler's totient function or Phi function. | ||
Time Complexity: O(sqrt(n)).""" | ||
result = n; | ||
for i in range(2, int(n ** 0.5) + 1): | ||
if n % i == 0: | ||
while n % i == 0: | ||
n //= i | ||
result -= result // i | ||
if n > 1: | ||
result -= result // n; | ||
return result; |
This file contains 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