From: Brian Candler Date: 2008-09-05T16:06:51+09:00 Subject: Re: Need a 40 LOC (ignoring comments) to be shorter -- suggestions wanted Damn this is annoying :-) OK here's another one, this time with a recursive permutation generator. It should be easier to modify to other variations of this problem. (However it currently doesn't permute arrays with duplicate elements) class Permutations include Enumerable def initialize(src) @data = src end def each(&blk) permute([], @data.to_a, &blk) end def permute(base, rest, &blk) if rest.empty? yield base else rest.each { |elem| permute(base + [elem], rest - [elem], &blk) } end end end p Permutations.new(3..7). map { |data| [data[0,3].join.to_i, data[3,2].join.to_i] }. min { |(a,b),(c,d)| a*b <=> c*d }