diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-23 13:23:49 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-23 13:23:49 +0000 |
| commit | 8bfe04ecd260f1c5294456eae74bcc582ded60df (patch) | |
| tree | 76e851374b947b7ba0c81ed830dd21f7b7ce3252 /challenge-144/sgreen/python | |
| parent | 204eb30b978d45b4d6b17adbf0ba69469cbbecb7 (diff) | |
| parent | a8bb8b3321e3967103e48cdbe331c49034fc5c04 (diff) | |
| download | perlweeklychallenge-club-8bfe04ecd260f1c5294456eae74bcc582ded60df.tar.gz perlweeklychallenge-club-8bfe04ecd260f1c5294456eae74bcc582ded60df.tar.bz2 perlweeklychallenge-club-8bfe04ecd260f1c5294456eae74bcc582ded60df.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-144/sgreen/python')
| -rwxr-xr-x | challenge-144/sgreen/python/ch-1.py | 36 | ||||
| -rwxr-xr-x | challenge-144/sgreen/python/ch-2.py | 37 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-144/sgreen/python/ch-1.py b/challenge-144/sgreen/python/ch-1.py new file mode 100755 index 0000000000..0301f27de9 --- /dev/null +++ b/challenge-144/sgreen/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +def get_primes(): + primes = [] + # Return a list of all primes between 2 and 50 (being 100 รท 2) + for i in range(2, 51): + for d in range(2, int(i / 2) + 1): + # If the number is divisable by something other than one and + # itself, it's not a prime + if i % d == 0: + break + else: + # It's a prime + primes.append(i) + + return primes + + +def main(): + primes = get_primes() + semiprimes = [] + + for i in primes: + for j in primes: + x = i * j + if x > 100: + break + if x not in semiprimes: + semiprimes.append(x) + + semiprimes.sort() + print(*semiprimes, sep=', ') + + +if __name__ == '__main__': + main() diff --git a/challenge-144/sgreen/python/ch-2.py b/challenge-144/sgreen/python/ch-2.py new file mode 100755 index 0000000000..086ad9a6f1 --- /dev/null +++ b/challenge-144/sgreen/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import math +import sys + + +def main(inputs): + m = int(inputs[0]) + n = int(inputs[1]) + + if m < 1: + raise ValueError('The first number must be a positive integer') + if n < 1: + raise ValueError('The second number must be a positive integer') + + # We know the first three values in the sequence are the two numbers and + # the sum + seq = [m, n, m + n] + current_value = seq[-1] + 1 + + while len(seq) < 10: + sums = 0 + for i in range(m, math.ceil(current_value / 2)): + j = current_value - i + if i in seq and j in seq: + sums += 1 + + if sums == 1: + seq.append(current_value) + + current_value += 1 + + print(*seq, sep=', ') + + +if __name__ == '__main__': + main(sys.argv[1:]) |
