Ask Your Question

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

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 close merge delete

## 1 Answer

Sort by ยป oldest newest most voted

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]]

more

## Comments

Thank you i will see the documentation

( 2018-09-04 16:36:37 +0200 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2018-09-04 03:45:31 +0200

Seen: 256 times

Last updated: Sep 04 '18