diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-10 11:49:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-10 11:49:38 +0100 |
| commit | bd27693f67067b9fab3a7de9b8b51848b41ab0c7 (patch) | |
| tree | 4602df187521d15dc110d26f0f0462ddf127397f /challenge-172/sgreen/python/ch-1.py | |
| parent | cc8580c1e95075ef7098044a075b037762ffd09a (diff) | |
| parent | 72aca2620e73ef772c725fd927e56960ef2fa964 (diff) | |
| download | perlweeklychallenge-club-bd27693f67067b9fab3a7de9b8b51848b41ab0c7.tar.gz perlweeklychallenge-club-bd27693f67067b9fab3a7de9b8b51848b41ab0c7.tar.bz2 perlweeklychallenge-club-bd27693f67067b9fab3a7de9b8b51848b41ab0c7.zip | |
Merge pull request #6412 from simongreen-net/master
sgreen solutions to challenge 172
Diffstat (limited to 'challenge-172/sgreen/python/ch-1.py')
| -rwxr-xr-x | challenge-172/sgreen/python/ch-1.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-172/sgreen/python/ch-1.py b/challenge-172/sgreen/python/ch-1.py new file mode 100755 index 0000000000..125ec64815 --- /dev/null +++ b/challenge-172/sgreen/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import sys +import math +from itertools import combinations + + +def is_prime(number): + '''Return true or false if the number is a prime''' + if number < 2: + return False + + for i in range(2, int(math.sqrt(number)) + 1): + if number % i == 0: + return False + + # It's a prime + return True + + +def main(m, n): + # Retrieve a list of all prime numbers <= m + primes = [x for x in range(m, 1, -1) if is_prime(x)] + + # Go through each combination of n length, and see if we have a solution + for l in combinations(primes, n): + if sum(l) == m: + print(l, sep=', ') + return + + # It is possible that no solution is found + print('No solution!') + + +if __name__ == '__main__': + main(int(sys.argv[1]), int(sys.argv[2])) |
