Ask Your Question
1

sage code to find sum of each combination of NcR

asked 2020-09-18 17:00:17 +0100

nirajverma gravatar image

updated 2020-09-18 18:58:18 +0100

tmonteil gravatar image

Ex :sage:Combinations([1,2,3,4,5],3).list() This will give a listing as follows

1,2,3
1,2,4
1,2,5
1,3,4
1,3,5
1,4,5
2,3,4
2,3,5
2,4,5
3,4,5

How to get a listing to include the sum of each combination alongside as follows

1,2,3  6
1,2,4  7

and so on My email id is : itsverma4u2@gmail.com

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-09-18 18:59:36 +0100

tmonteil gravatar image

You can iterate over the combinations with a for loop and then compute the sum of the elements as follows:

sage: for i in Combinations([1,2,3,4,5],3): 
....:     print(i, sum(i)) 
....:                                                                                                                                                                                                        
[1, 2, 3] 6
[1, 2, 4] 7
[1, 2, 5] 8
[1, 3, 4] 8
[1, 3, 5] 9
[1, 4, 5] 10
[2, 3, 4] 9
[2, 3, 5] 10
[2, 4, 5] 11
[3, 4, 5] 12
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2020-09-18 17:00:17 +0100

Seen: 267 times

Last updated: Sep 18 '20