Skip to content
Prev 263003 / 398502 Next

can this sequence be generated easier?

Here ia an idea that might be useful to adapt

fixedSumCombinations <- function(N, terms) 
	if(terms == 1) return(N) else
	if(terms == 2) return(cbind(0:N, N:0)) else {
	X <- NULL
	for(i in 0:N) 
		X <- rbind(X, cbind(i, Recall(N-i, terms-1)))
	X
}

example:
i    
 [1,] 0 0 4
 [2,] 0 1 3
 [3,] 0 2 2
 [4,] 0 3 1
 [5,] 0 4 0
 [6,] 1 0 3
 [7,] 1 1 2
 [8,] 1 2 1
 [9,] 1 3 0
[10,] 2 0 2
[11,] 2 1 1
[12,] 2 2 0
[13,] 3 0 1
[14,] 3 1 0
[15,] 4 0 0

Bill Venables.