From cced4df12336d27ff6b3186c66fece1deb6fa9bd Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 9 Jul 2022 23:24:05 +0100 Subject: - Added guest contributions by Laurent Rosenfeld. --- challenge-172/laurent-rosenfeld/python/ch-1.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-172/laurent-rosenfeld/python/ch-1.py (limited to 'challenge-172/laurent-rosenfeld/python/ch-1.py') diff --git a/challenge-172/laurent-rosenfeld/python/ch-1.py b/challenge-172/laurent-rosenfeld/python/ch-1.py new file mode 100644 index 0000000000..098213199f --- /dev/null +++ b/challenge-172/laurent-rosenfeld/python/ch-1.py @@ -0,0 +1,25 @@ +import math +from itertools import combinations + +def is_prime(n): + if n == 2: + return True + if n == 0 or n == 1 or n % 2 == 0: + return False + p = 3 + sqrt_n = math.sqrt(n) + while (p <= sqrt_n): + if ((n % p) == 0): + return False + p += 2 + return True + +def partition(m, n): + primes = filter(is_prime, range (1, m)) + for combination in combinations(primes, n): + if sum(combination) == m: + print(m, n, ": ", combination) + +partition(18, 2) +partition(19, 3) +partition(25, 2) -- cgit