aboutsummaryrefslogtreecommitdiff
path: root/challenge-175/mohammad-anwar/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-175/mohammad-anwar/python')
-rw-r--r--challenge-175/mohammad-anwar/python/ch-2.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-175/mohammad-anwar/python/ch-2.py b/challenge-175/mohammad-anwar/python/ch-2.py
new file mode 100644
index 0000000000..70ff32d7e2
--- /dev/null
+++ b/challenge-175/mohammad-anwar/python/ch-2.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python3
+
+'''
+
+Week 175:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-175
+
+Task #2: Perfect Totient Numbers
+
+ Write a script to generate first 20 Perfect Totient Numbers.
+
+'''
+
+import math
+import unittest
+
+def isCoprime(a, b) -> bool:
+ return math.gcd(a, b) == 1
+
+#
+# Simply coded as shown in the example in the wiki page:
+# https://en.wikipedia.org/wiki/Perfect_totient_number
+
+def isPerfectTotient(n) -> bool:
+ i = n
+ s = 0
+ while i >= 1:
+ coprimes = []
+ for j in range(1, i):
+ if isCoprime(i, j):
+ coprimes.append(j)
+
+ i = len(coprimes)
+ s = s + i
+
+ return n == s
+
+def firstPerfectTotient(n):
+ pt = []
+ i = 1
+ while (len(pt) < n):
+ if isPerfectTotient(i):
+ pt.append(i)
+ i = i + 1
+
+ return pt
+
+#
+#
+# Unit test class
+
+class TestPerfectTotient(unittest.TestCase):
+ def test_firstPerfectTotient(self):
+ exp = [
+ 3, 9, 15, 27, 39, 81, 111, 183, 243,
+ 255, 327, 363, 471, 729, 2187, 2199,
+ 3063, 4359, 4375, 5571
+ ]
+ got = firstPerfectTotient(20)
+ self.assertEqual(exp, got)
+
+unittest.main()