Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Calculating the unique combinations of four numbers that add up to 40.
require 'set'
def calculate_combos(limit)
combos = SortedSet.new
range_limit = (1..limit).to_a
range_limit.each do |w|
range_limit.each do |x|
range_limit.each do |y|
range_limit.each do |z|
combos.add([w, x, y, z].sort) if w + x + y + z == limit
end
end
end
end
combos
end
combos = calculate_combos(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
Something went wrong with that request. Please try again.