diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-26 18:03:28 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-26 18:03:28 +0100 |
| commit | 2540be5381f080473f9bdabb1b78ebbe7aae178d (patch) | |
| tree | 0463490c1537032d7d17fe61e8c049c95f69c970 /challenge-170/mohammad-anwar/python | |
| parent | 7ccae39ceac6e4ad1bb83bd3e9b169deddfc02f0 (diff) | |
| download | perlweeklychallenge-club-2540be5381f080473f9bdabb1b78ebbe7aae178d.tar.gz perlweeklychallenge-club-2540be5381f080473f9bdabb1b78ebbe7aae178d.tar.bz2 perlweeklychallenge-club-2540be5381f080473f9bdabb1b78ebbe7aae178d.zip | |
- Added Python solution to the task "Primorial Numbers" of week 170.
Diffstat (limited to 'challenge-170/mohammad-anwar/python')
| -rw-r--r-- | challenge-170/mohammad-anwar/python/ch-1.py | 56 |
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() |
