# Same sum, different sets of integers

5 messages
Open this post in threaded view
|

## 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 ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Same sum, different sets of integers

Open this post in threaded view
|

## Re: Same sum, different sets of integers

 In reply to this post by Atte Tenkanen 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 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, ", "", strings) > > # print out > cat(strings, sep = '\n') 1, 7 2, 6 3, 5 4, 4 1, 1, 6 1, 2, 5 1, 3, 4 2, 2, 4 2, 3, 3 1, 1, 1, 5 1, 1, 2, 4 1, 1, 3, 3 1, 2, 2, 3 2, 2, 2, 2 1, 1, 1, 1, 4 1, 1, 1, 2, 3 1, 1, 2, 2, 2 1, 1, 1, 1, 1, 3 1, 1, 1, 1, 2, 2 1, 1, 1, 1, 1, 1, 2 1, 1, 1, 1, 1, 1, 1, 1 Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Wed, Apr 27, 2016 at 11:46 AM, Atte Tenkanen <[hidden email]> wrote: > 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 > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.