Question

Using SageMath (or Python) count the number of subets in the range {1-30} whose elements sum...

Using SageMath (or Python) count the number of subets in the range {1-30} whose elements sum to 30.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem in Python. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code


#method to find how many subsets in the range (n,30) whose elements sum to 30
#first paremeter is n - which is the starting value, second is sum - which holds
#the sum of elements in current subset, third parameter is optional, just included
#to display the subsets if you want to see them

def countSubsetSum30(n, sum, str_=''):
    #if sum is 30, returns 1 (base condition)
   
if sum==30:
        #if you want to see all the subsets that sum upto 30, uncomment below line
        #print(str_)
       
return 1
    #otherwise, if the sum exceeds 30 or n exceeds 30, returning 0
   
elif sum>30 or n>30:
        return 0
    #otherwise counting subsets that sum to 30 with current element included and without
    #current element included, summing and returning the result
   
return countSubsetSum30(n+1,sum+n,str_+str(n)+' ')+countSubsetSum30(n+1,sum,str_)


#finding the count of subsets in range 1,30 that sum to 30, should be 296
print('Count:',countSubsetSum30(1,0))

#output

Count: 296

Add a comment
Know the answer?
Add Answer to:
Using SageMath (or Python) count the number of subets in the range {1-30} whose elements sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT