Efficiently determining maximum allowed euclidean distance between lists of colors
As @vnp says, a solution involving an octree would a more efficient way to find neighboring points.

Before you implement one, though, there are some huge time savings you can get by making some trivial changes:


Computing square roots is computationally intensive.  Do it only when you need to.
Parsing hex values into integers is somewhat slow, especially when you do it using a lot of string and array operations.  It would be more efficient to parse the entire 6-hex-digit string into an integer, then use bit manipulation.
The same goes for generating random colors.  I don't know why you used rand(254) rather than rand(256).  If you allow color components FE and FF, then it becomes a simple matter of generating a 24-bit random number, and formatting it as hex.  (Better yet, avoid converting the colors into their hex representation altogether, and just store them as integers.)


In addition, 1.upto(10000) { color_list &lt;&lt; random_color } is poor Ruby style.  Write it using .map instead (like you did in the implementation of random_colors).

I don't know why you wrote results[t] ||= [] instead of results[t] = [].

With just those changes, I'm able to reduce the execution time by 90% already.

def color_dist_sq(c1, c2)
  c1 = c1.to_i(16)
  c2 = c2.to_i(16)
  [0, 8, 16].map do |bits|
    ((c1 &gt;&gt; bits &amp; 0xff) - (c2 &gt;&gt; bits &amp; 0xff)) ** 2
  end.inject(:+)
end

def random_color
  '%06x' % rand(1 &lt;&lt; 24)
end

TARGET_DISTANCE_SQ = 10**2
color_list = 10000.times.map { random_color }
target_colors = 100.times.map { random_color }

results = {}
target_colors.each do |t|
  results[t] = color_list.inject([]) do |result, c|
    dist_sq = color_dist_sq(c, t)
    result &lt;&lt; [c, Math.sqrt(dist_sq)] if dist_sq &lt;= TARGET_DISTANCE_SQ
    result
  end
end