Skip to content

Commit 62a3ba2

Browse files
goswami-rahulkeon
authored andcommitted
add Euler's Totient function (keon#421)
* add Euler's Totient function * add link to Euler's totient function in README
1 parent 89aabde commit 62a3ba2

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ If you want to uninstall algorithms, it is as simple as:
186186
- [base_conversion](algorithms/maths/base_conversion.py)
187187
- [combination](algorithms/maths/combination.py)
188188
- [decimal_to_binary_ip](algorithms/maths/decimal_to_binary_ip.py)
189+
- [euler_totient](algorithms/maths/euler_totient.py)
189190
- [extended_gcd](algorithms/maths/extended_gcd.py)
190191
- [factorial](algorithms/maths/factorial.py)
191192
- [gcd/lcm](algorithms/maths/gcd.py)

algorithms/maths/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .base_conversion import *
22
from .decimal_to_binary_ip import *
3+
from .euler_totient import *
34
from .extended_gcd import *
45
from .factorial import *
56
from .gcd import *

algorithms/maths/euler_totient.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Euler's totient function, also known as phi-function ϕ(n),
3+
counts the number of integers between 1 and n inclusive,
4+
which are coprime to n.
5+
(Two numbers are coprime if their greatest common divisor (GCD) equals 1).
6+
"""
7+
def euler_totient(n):
8+
"""Euler's totient function or Phi function.
9+
Time Complexity: O(sqrt(n))."""
10+
result = n;
11+
for i in range(2, int(n ** 0.5) + 1):
12+
if n % i == 0:
13+
while n % i == 0:
14+
n //= i
15+
result -= result // i
16+
if n > 1:
17+
result -= result // n;
18+
return result;

tests/test_maths.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from algorithms.maths import (
22
int_to_base, base_to_int,
33
decimal_to_binary_ip,
4+
euler_totient,
45
extended_gcd,
56
factorial, factorial_recur,
67
gcd, lcm,
@@ -36,8 +37,8 @@ def test_base_to_int(self):
3637
self.assertEqual(5, base_to_int("101", 2))
3738
self.assertEqual(0, base_to_int("0", 2))
3839
self.assertEqual(255, base_to_int("FF", 16))
39-
40-
40+
41+
4142
class TestDecimalToBinaryIP(unittest.TestCase):
4243
"""
4344
Test for the file decimal_to_binary_ip.py
@@ -55,6 +56,21 @@ def test_decimal_to_binary_ip(self):
5556
decimal_to_binary_ip("192.168.0.1"))
5657

5758

59+
class TestEulerTotient(unittest.TestCase):
60+
"""[summary]
61+
Test for the file euler_totient.py
62+
63+
Arguments:
64+
unittest {[type]} -- [description]
65+
"""
66+
67+
def test_euler_totient(self):
68+
self.assertEqual(4, euler_totient(8))
69+
self.assertEqual(12, euler_totient(21))
70+
self.assertEqual(311040, euler_totient(674614))
71+
self.assertEqual(2354352, euler_totient(3435145))
72+
73+
5874
class TestExtendedGcd(unittest.TestCase):
5975
"""[summary]
6076
Test for the file extended_gcd.py
@@ -274,4 +290,3 @@ def test_factorial_recur(self):
274290

275291
if __name__ == "__main__":
276292
unittest.main()
277-

0 commit comments

Comments
 (0)