From: Carl Porth Date: 2007-04-09T07:12:32+09:00 Subject: Re: Getting to 100 (#119) After going back and reading the current solutions, I like Ken Bloom's each_partition method. It's much cleaner than my combinations method. On Apr 8, 2:59 pm, "Carl Porth" wrote: > here is my first pass: > > class Array > def combinations(n) > case n > when 0: [] > when 1: self.map { |e| [e] } > when size: [self] > else > (0..(size - n)).to_a.inject([]) do |mem,i| > mem += self[(i+1)..size].combinations(n-1).map do |rest| > [self[i],*rest] > end > end > end > end > end > > equations = 0 > separator = "************************" > > (1..8).to_a.combinations(3).each do |partitions| > 3.times do |n| > equation = "123456789" > > partitions.reverse.each_with_index do |partition,index| > equation = equation.insert(partition, (index == n ? ' + ' : ' - > ')) > end > > result = eval(equation) > equation << " = #{result}" > > if result == 100 > equation = "#{separator}\n#{equation}\n#{separator}" > end > > puts equation > > equations += 1 > end > end > > puts "#{equations} possible equations tested"