Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
If we assume each weight must be different...
def calculate_combination(num_elements, limit)
combos =
base_array = (1..limit).to_a
base_array.combination(num_elements).each do | group |
combos.add(group.sort) if group.inject(0, :+) == limit
combos = calculate_combination(4, 40)
combos.each { |weight_combo|
weight_combo.each { |val| print "#{val} " }
print "\n"
puts combos.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment