Displaying 3 results from an estimated 3 matches for "add2n".
Did you mean:
add2
2016 Apr 28
0
Same sum, different sets of integers
...gt; # create expand.grid for all combinations and convert to matrix
> x <- as.matrix(expand.grid(rep(list(0:(N - 1)), N)))
>
> # generate rowSums and determine which rows add to N
> z <- rowSums(x)
>
> # now extract those rows, sort and convert to strings to remove dups
> add2N <- x[z == N, ]
> strings <- apply(
+ t(apply(add2N, 1, sort)) # sort
+ , 1
+ , toString
+ )
>
> # remove dups
> strings <- strings[!duplicated(strings)]
>
> # remove leading zeros
> strings <- gsub("0, ", &q...
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
2016 Apr 28
1
Same sum, different sets of integers
...all combinations and convert to matrix
>> x <- as.matrix(expand.grid(rep(list(0:(N - 1)), N)))
>>
>> # generate rowSums and determine which rows add to N
>> z <- rowSums(x)
>>
>> # now extract those rows, sort and convert to strings to remove dups
>> add2N <- x[z == N, ]
>> strings <- apply(
> + t(apply(add2N, 1, sort)) # sort
> + , 1
> + , toString
> + )
>>
>> # remove dups
>> strings <- strings[!duplicated(strings)]
>>
>> # remove leading zeros...