[R] Same sum, different sets of integers
Jim Lemon
drjimlemon at gmail.com
Thu Apr 28 02:21:27 CEST 2016
Hi Atte,
I'm not sure that this actually works, and it's very much a quick hack:
sums_x<-function(x,addends=1,depth=1) {
if(depth==1) {
addends<-rep(addends,x)
addlist<-list(addends)
} else {
addlist<-list()
}
lenadd<-length(addends)
while(lenadd > 2) {
addends<-c(addends[depth]+1,addends[-c(depth,depth+1)])
lenadd<-lenadd-1
if(sum(addends) == x) addlist[[length(addlist)+1]]<-addends
cat(depth,"-",addends,"\n")
if(lenadd > 2 && depth+1 < lenadd)
addlist<-c(addlist,(sums_x(x,addends=addends,depth=depth+1)))
}
return(addlist)
}
This doesn't return all the permutations of the addends, but it's all
the time I have to waste this morning.
Jim
On Thu, Apr 28, 2016 at 1:46 AM, Atte Tenkanen <attenka at utu.fi> 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
>
> ______________________________________________
> R-help at r-project.org 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.
More information about the R-help
mailing list