Displaying 1 result from an estimated 1 matches for "barlin".
Did you mean:
karlin
2016 Apr 28
1
Same sum, different sets of integers
I came up with this, using recursion. Short and should work for n
greater than 9 :)
Peter
sumsToN = function(n)
{
if (n==1) return(1);
out = lapply(1:(n-1), function(i) {
s1 = sumsToN(n-i);
lapply(s1, c, i)
})
c(n, unlist(out, recursive = FALSE));
}
> sumsToN(4)
[[1]]
[1] 4
[[2]]
[1] 3 1
[[3]]
[1] 2 1 1
[[4]]
[1] 1 1 1 1
[[5]]
[1] 1 2 1
[[6]]
[1] 2 2
[[7]]
[1] 1 1 2