I have to complete a problem that states: Given a number'n', and a list of prime numbers 'p_list' (in ascending order),find a sum that use some combination of numbers in p_list that addup to 'n.' For example if n = 17 and p_list = [2, 3, 5], one sum is2+5+5+5 = 17.
I am also given two functions:
def sum_exists(n, i = 0, p_list)
def find_sum(n, i = 0, p_list, sum_list)
In the first function should return True if there exists acombination of numbers from p_list that add up to 'n' and False ifnone exists. It should be implemented using recursion.
The second function should return an expression of a sum ofnumbers from p_list that add up to 'n' in the form of a list. If nosum exists, return an empty list. I don't have to find all thesums, just the first one will suffice. Same as the first function,it should be implemented using recursion.
The p_list can be any numbers but 'n' should be a prime numberto see if the numbers add to it. Here are two p_list.
n 17 7 107 43 146 P list [2, 3, 5] [3, 5] [2, 3, 37] [3, 29] [17, 29, 37] Expected Result True False True False True
n 17 7 107 43 146 P list [2, 3, 5] [3, 5] [[2, 3, 37] [3, 29] [17, 29, 37] Expected Result [2, 2, 2, 2, 2, 2, 2, 3] [] [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3] [] [17, 17, 17, 29, 29, 37] 2, 2, 2, 2, 2, 2, N N N N 2,
I have to complete a problem that states: Given a number 'n', and a list of prime numbers 'p_list' (in ascending order),
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am