Ask Your Question
0

How to get a list of the combinations such that the elements are equal to some $n$

asked 2018-09-04 03:45:31 +0100

MarioM gravatar image

updated 2018-09-04 03:47:37 +0100

I want a generalization of this kind

[(r1, ..., rk) for r1 in range(n+1) ... for rk in range(n+1) if r1 + ... + rk = n]

For a general number of $r$'s and any $n$

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2018-09-04 07:47:34 +0100

tmonteil gravatar image

You can use IntegerListsLex (see the documentation for all options):

sage: C = IntegerListsLex(6, length=4)

sage: C.list()
[[6, 0, 0, 0],
 [5, 1, 0, 0],
 [5, 0, 1, 0],
 [5, 0, 0, 1],
 [4, 2, 0, 0],
 [4, 1, 1, 0],
 [4, 1, 0, 1],
 [4, 0, 2, 0],
 [4, 0, 1, 1],
 [4, 0, 0, 2],
 [3, 3, 0, 0],
 [3, 2, 1, 0],
 [3, 2, 0, 1],
 [3, 1, 2, 0],
 [3, 1, 1, 1],
 [3, 1, 0, 2],
 [3, 0, 3, 0],
 [3, 0, 2, 1],
 [3, 0, 1, 2],
 [3, 0, 0, 3],
 [2, 4, 0, 0],
 [2, 3, 1, 0],
 [2, 3, 0, 1],
 [2, 2, 2, 0],
 [2, 2, 1, 1],
 [2, 2, 0, 2],
 [2, 1, 3, 0],
 [2, 1, 2, 1],
 [2, 1, 1, 2],
 [2, 1, 0, 3],
 [2, 0, 4, 0],
 [2, 0, 3, 1],
 [2, 0, 2, 2],
 [2, 0, 1, 3],
 [2, 0, 0, 4],
 [1, 5, 0, 0],
 [1, 4, 1, 0],
 [1, 4, 0, 1],
 [1, 3, 2, 0],
 [1, 3, 1, 1],
 [1, 3, 0, 2],
 [1, 2, 3, 0],
 [1, 2, 2, 1],
 [1, 2, 1, 2],
 [1, 2, 0, 3],
 [1, 1, 4, 0],
 [1, 1, 3, 1],
 [1, 1, 2, 2],
 [1, 1, 1, 3],
 [1, 1, 0, 4],
 [1, 0, 5, 0],
 [1, 0, 4, 1],
 [1, 0, 3, 2],
 [1, 0, 2, 3],
 [1, 0, 1, 4],
 [1, 0, 0, 5],
 [0, 6, 0, 0],
 [0, 5, 1, 0],
 [0, 5, 0, 1],
 [0, 4, 2, 0],
 [0, 4, 1, 1],
 [0, 4, 0, 2],
 [0, 3, 3, 0],
 [0, 3, 2, 1],
 [0, 3, 1, 2],
 [0, 3, 0, 3],
 [0, 2, 4, 0],
 [0, 2, 3, 1],
 [0, 2, 2, 2],
 [0, 2, 1, 3],
 [0, 2, 0, 4],
 [0, 1, 5, 0],
 [0, 1, 4, 1],
 [0, 1, 3, 2],
 [0, 1, 2, 3],
 [0, 1, 1, 4],
 [0, 1, 0, 5],
 [0, 0, 6, 0],
 [0, 0, 5, 1],
 [0, 0, 4, 2],
 [0, 0, 3, 3],
 [0, 0, 2, 4],
 [0, 0, 1, 5],
 [0, 0, 0, 6]]
edit flag offensive delete link more

Comments

Thank you i will see the documentation

MarioM gravatar imageMarioM ( 2018-09-04 16:36:37 +0100 )edit

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: 2018-09-04 03:45:31 +0100

Seen: 341 times

Last updated: Sep 04 '18