From: Hae Lee Date: 2008-11-06T23:51:33+09:00 Subject: Re: Combination of numbers in an array that add up to x Andreas Launila wrote: > The simplest way to solve this problem in Gecode/R is probably to use > set variables: > > require 'rubygems' > require 'gecoder' > > weights = [2429.63, 497.87, 51.96, 59.43, 138.4, 66.22, 28.74, 1.75, > 2075.13, 556.14, 112.56, 116.5, 84.41, 55.97, 139.07, 24.46] > # Convert to integers. > weights.map!{ |x| (x*100).floor } > > solution = Gecode.solve do > selected_weights_is_a set_var([], weights) > selected_weights.sum.must == 343578 > > branch_on selected_weights > end > p solution.selected_weights.value.map{ |x| x.to_f / 100 } > > Output: > > [1.75, 112.56, 116.5, 138.4, 139.07, 497.87, 2429.63] > > The variable "selected_weights" is a set variable that may include any > of the elements in "weights". A constraint is then placed on the sum of > that set. Hi there; thanks so much! Qq (quick question): How would I modify this so that it lists each possible combination surrounded within their own bracket sets? I modified the weight and sum values to something I know that'd have more than one combination, but it only spat back out just one result set. As you can tell, I'm less than a newb to coding and Ruby! Sorry for the inconvenience! And again, thanks! -- Posted via http://www.ruby-forum.com/.