sum of prime numbers
Question:
I have an exercise:
Write a program that takes the number n from the input and prints all cases of prime numbers whose sum is equal to n.
For example: input is n = 13 and output is:
2 2 2 2 2 3
2 2 2 2 5
2 2 2 7
2 2 3 3 3
2 3 3 5
2 11
3 3 7
3 5 5
13
The output is sorted by lexicographic method.
I just could code for finding the prime numbers between 1-n:
n = int(input())
lst = []
for i in range(2, n + 1):
isPrime = True
for j in range(2, i - 1):
if i % j == 0:
isPrime = False
if isPrime:
lst.append(i)
print(lst)
Answers:
Here is my take at the problem.
solution = []
primes_up_to_n = [2, 3, 5, 7, 11, 13]
def get_sums(primes, n, path):
global solution # keep track of solutions found so far.
if n < min(primes):
return # no more solutions are possible at this point
for prime in primes:
if n == prime:
solution.append(path+[prime])
get_sums(primes, n-prime, path+[prime])
get_sums(primes_up_to_n , 13, [])
k = [sorted(x) for x in solution] # order the solutions
print([*map(list, {*map(tuple, k)})]) # remove the duplicates
Explanation:
- we check if the current n is in the prime list. If it is, it means that
path + [n]
is a solution, so we append it.
- we reccursively call
get_sums
to find further solutions.
The problem with this approach is that the algorithm doesn’t differentiate between [2, 2, 2, 2, 2, 3]
and [2, 2, 2, 2, 3, 2]
for instance. So we get rid of such duplicates in the last two lines.
The code outputs:
[[2, 2, 2, 2, 2, 3],
[2, 2, 2, 7],
[2, 3, 3, 5],
[13],
[3, 3, 7],
[2, 2, 3, 3, 3],
[3, 5, 5],
[2, 2, 2, 2, 5],
[2, 11]]
I have an exercise:
Write a program that takes the number n from the input and prints all cases of prime numbers whose sum is equal to n.
For example: input is n = 13 and output is:
2 2 2 2 2 3
2 2 2 2 5
2 2 2 7
2 2 3 3 3
2 3 3 5
2 11
3 3 7
3 5 5
13
The output is sorted by lexicographic method.
I just could code for finding the prime numbers between 1-n:
n = int(input())
lst = []
for i in range(2, n + 1):
isPrime = True
for j in range(2, i - 1):
if i % j == 0:
isPrime = False
if isPrime:
lst.append(i)
print(lst)
Here is my take at the problem.
solution = []
primes_up_to_n = [2, 3, 5, 7, 11, 13]
def get_sums(primes, n, path):
global solution # keep track of solutions found so far.
if n < min(primes):
return # no more solutions are possible at this point
for prime in primes:
if n == prime:
solution.append(path+[prime])
get_sums(primes, n-prime, path+[prime])
get_sums(primes_up_to_n , 13, [])
k = [sorted(x) for x in solution] # order the solutions
print([*map(list, {*map(tuple, k)})]) # remove the duplicates
Explanation:
- we check if the current n is in the prime list. If it is, it means that
path + [n]
is a solution, so we append it. - we reccursively call
get_sums
to find further solutions.
The problem with this approach is that the algorithm doesn’t differentiate between[2, 2, 2, 2, 2, 3]
and[2, 2, 2, 2, 3, 2]
for instance. So we get rid of such duplicates in the last two lines.
The code outputs:
[[2, 2, 2, 2, 2, 3],
[2, 2, 2, 7],
[2, 3, 3, 5],
[13],
[3, 3, 7],
[2, 2, 3, 3, 3],
[3, 5, 5],
[2, 2, 2, 2, 5],
[2, 11]]