aboutsummaryrefslogtreecommitdiff
path: root/challenge-175/mohammad-anwar/python/ch-2.py
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-07-30 01:07:04 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-07-30 01:07:04 +0100
commit40550e1e6ea41dabe7a6a72e17f5f56de85fabcd (patch)
treee170a85111547d5b4e09224fe56ecf6788726a6f /challenge-175/mohammad-anwar/python/ch-2.py
parentd69c546a4fda62fcd899662f52a45e3ba03365c3 (diff)
parentc549a7288ca66b1b48e8518454806b538fd97568 (diff)
downloadperlweeklychallenge-club-40550e1e6ea41dabe7a6a72e17f5f56de85fabcd.tar.gz
perlweeklychallenge-club-40550e1e6ea41dabe7a6a72e17f5f56de85fabcd.tar.bz2
perlweeklychallenge-club-40550e1e6ea41dabe7a6a72e17f5f56de85fabcd.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-175/mohammad-anwar/python/ch-2.py')
-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()