aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-06-26 18:03:28 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-06-26 18:03:28 +0100
commit2540be5381f080473f9bdabb1b78ebbe7aae178d (patch)
tree0463490c1537032d7d17fe61e8c049c95f69c970
parent7ccae39ceac6e4ad1bb83bd3e9b169deddfc02f0 (diff)
downloadperlweeklychallenge-club-2540be5381f080473f9bdabb1b78ebbe7aae178d.tar.gz
perlweeklychallenge-club-2540be5381f080473f9bdabb1b78ebbe7aae178d.tar.bz2
perlweeklychallenge-club-2540be5381f080473f9bdabb1b78ebbe7aae178d.zip
- Added Python solution to the task "Primorial Numbers" of week 170.
-rw-r--r--challenge-170/mohammad-anwar/python/ch-1.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-170/mohammad-anwar/python/ch-1.py b/challenge-170/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..705da3fa78
--- /dev/null
+++ b/challenge-170/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,56 @@
+#!/usr/bin/python3
+
+'''
+
+Week 170:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-170
+
+Task #1: Primorial Numbers
+
+ Write a script to generate first 10 Primorial Numbers.
+
+'''
+
+import math
+import unittest
+
+def is_prime(n):
+ if n == 1:
+ return 0
+
+ i = 2
+ while (i <= int(math.sqrt(n))):
+ if ((n % i) == 0):
+ return 0
+ i += 1
+
+ return 1
+
+def primorial_numbers(n):
+ pn = []
+ i = 0
+ j = 1
+ while (len(pn) < n):
+ i = i + 1
+ if (is_prime(i) != 1):
+ continue
+ j = i * j
+ pn.append(j)
+
+ return pn
+
+#
+#
+# Unit test class
+
+class TestPrimorialNumbers(unittest.TestCase):
+
+ def test_example(self):
+ self.assertEqual(
+ primorial_numbers(10),
+ [2, 6, 30, 210, 2310, 30030, 510510, 9699690, 223092870, 6469693230],
+ 'Example'
+ )
+
+unittest.main()