public
Created

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

  • Download Gist
combos.rb
Ruby
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.