Displaying 3 results from an estimated 3 matches for "sumston".
Did you mean:
juston
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
[...
2016 Apr 28
0
Same sum, different sets of integers
This is not the most efficient, but gets the idea across. This is the
largest sum I can compute on my laptop with 16GB of memory. If I try to
set N to 9, I run out of memory due to the size of the expand.grid.
> N <- 8 # value to add up to
> # create expand.grid for all combinations and convert to matrix
> x <- as.matrix(expand.grid(rep(list(0:(N - 1)), N)))
>
> # generate
2016 Apr 27
3
Same sum, different sets of integers
Hi,
Do you have ideas, how to find all those different combinations of
integers (>0) that produce as a sum, a certain integer.
i.e.: if that sum is
3, the possibilities are c(1,1,1), c(1,2), c(2,1)
4, the possibilities are
c(1,1,1,1),c(1,1,2),c(1,2,1),c(2,1,1),c(2,2),c(1,3),c(3,1)
etc.
Best regards,
Atte Tenkanen