Skip to content

Instantly share code, notes, and snippets.

@RichardHJensen
Created February 23, 2012 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardHJensen/1894024 to your computer and use it in GitHub Desktop.
Save RichardHJensen/1894024 to your computer and use it in GitHub Desktop.
If we assume each weight must be different...
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment