I have to complete a problem that states: Given a number 'n', and a list of prime numbers 'p_list' (in ascending order),

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

I have to complete a problem that states: Given a number 'n', and a list of prime numbers 'p_list' (in ascending order),

Post by answerhappygod »

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.
I Have To Complete A Problem That States Given A Number N And A List Of Prime Numbers P List In Ascending Order 1
I Have To Complete A Problem That States Given A Number N And A List Of Prime Numbers P List In Ascending Order 1 (57.06 KiB) Viewed 43 times
I Have To Complete A Problem That States Given A Number N And A List Of Prime Numbers P List In Ascending Order 2
I Have To Complete A Problem That States Given A Number N And A List Of Prime Numbers P List In Ascending Order 2 (111.67 KiB) Viewed 43 times
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,
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply