Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Calculating the unique combinations of four numbers that add up to 40.

View combos.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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.