Function that contiguously enumerates all positive integer sequences of length $x$ that sum to $y$ I am looking for a function that contiguously enumerates all positive integer sequences of length $x$ that sum to a positive integer value $y$. By "contiguously enumerate" I mean that the function needs to map all possible sequences to a unique positive integer value, such that there is no output integer value in the range of $[1,n]$ that does not have a corresponding input sequence. $n$ is the number of possible sequences.
More formally, I'm looking for a bijective function $F(x,y,S) \to \mathbb{Z}^{+}$ where:

*

*$S$ is valid sequence. $S$ is valid if:

*

*$|S|=x$

*$\forall s \in S, (s \in \mathbb{Z}) \land (s \geq 0)$

*$\sum_{i=1}^{x}S_i = y$



*$\forall$ valid $S$, $F(x,y,S) \in [1,n]$ where $n=|\{$ all valid sequences $\}|$
Here's an example of the behavior I'm looking for:
F(3, 3, [0,0,3]) = 1
F(3, 3, [0,1,2]) = 2
F(3, 3, [0,2,1]) = 3
F(3, 3, [0,3,0]) = 4
F(3, 3, [1,0,2]) = 5
F(3, 3, [1,1,1]) = 6
F(3, 3, [1,2,0]) = 7
F(3, 3, [2,0,1]) = 8
F(3, 3, [2,1,0]) = 9
F(3, 3, [3,0,0]) = 10

Just to be extra clear, these are sequences, so the order of their elements is significant. [3,0,0] is not treated the same as [0,0,3].
EDIT: Here's an iterative version of @Phicar's solution in Python:
def F(x, y, S):
    result = 0
    i = 0
    _x = x
    for i in range(_x - 1):
        cc = S[i]
        if cc > 0:
            result += binomial(y + x - 1, x - 1) - binomial(y - cc + x - 1, x - 1)
        x -= 1
        y -= cc
    return result

(This doesn't account for the case where $x=1$ or $y=0$)
 A: Recursively it looks like this: If your sequence has length $1,$ treat this as a base case and do accordingly, if not, select the first element in $S$ i.e., $S_1.$ If $S_1=0,$ go to the next index(nothing has gone before this so far). If not, consider $\binom{y-i+x-2}{x-2}$ elements that have to go before this one for a fixed $i$ (as noticed in the comments by Hagen von Eitzen) for $i=0$ all the way to $S_1-1,$ hence you want to push $$\binom{y+x-1}{x-1}-\binom{y-S_1+x-1}{x-1}.$$
This last formula is just a consequence of the hockey-stick identity.  
The algorithm, in sage, then looks like :
def tal(a,b,c):
    if len(c)==1:
        if a==1:
            if b==c[0]:
                return 1
            return 0
        return 0
    d = [0]*(len(c)-1)
    for i in range(1,len(c)):
        d[i-1]=c[i]
    cc = c[0]
    push = 0
    if cc>0:
        push = binomial(b+a-1,a-1)-binomial(b-cc+a-1,a-1)
    return push+tal(a-1,b-cc,d)
print(tal(3,3,[2,0,1]))

where $a$ is the original $x,$ $b$ is $y$ and $c$ is the sequence.
