Created
February 23, 2012 17:53
-
-
Save RichardHJensen/1894024 to your computer and use it in GitHub Desktop.
If we assume each weight must be different...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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