public

If we assume each weight must be different...

  • Download Gist
calc_with_combination.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def calculate_combination(num_elements, limit)
combos = SortedSet.new
base_array = (1..limit).to_a
base_array.combination(num_elements).each do | group |
combos.add(group.sort) if group.inject(0, :+) == limit
end
combos
end
 
combos = calculate_combination(4, 40)
combos.each { |weight_combo|
weight_combo.each { |val| print "#{val} " }
print "\n"
}
puts combos.size

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.