Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

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

asked 6 years ago

MarioM gravatar image

updated 6 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 6 years ago

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]]
Preview: (hide)
link

Comments

Thank you i will see the documentation

MarioM gravatar imageMarioM ( 6 years ago )

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: 6 years ago

Seen: 373 times

Last updated: Sep 04 '18