aboutsummaryrefslogtreecommitdiff
path: root/challenge-175/mohammad-anwar/python
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-29 19:00:29 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-29 19:00:29 +0100
commit148cacf688e1c0e2d191362653c01db93cae56cb (patch)
tree4c0b3f4c07fd8804d11aa85b8dcffdef9cf0b764 /challenge-175/mohammad-anwar/python
parent2ba2a46f6d2eb7ecc4d3db088505df6e86665e75 (diff)
downloadperlweeklychallenge-club-148cacf688e1c0e2d191362653c01db93cae56cb.tar.gz
perlweeklychallenge-club-148cacf688e1c0e2d191362653c01db93cae56cb.tar.bz2
perlweeklychallenge-club-148cacf688e1c0e2d191362653c01db93cae56cb.zip
- Added solutions to the task "Perfect Totient Numbers".
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()