From: draq Date: 2005-12-13T02:02:58+09:00 Subject: Re: Calculating single-digit summands This is a newer algorithm which works much more faster. def sum (arr) # sorry James Edward Gray II, but I'm not using an enum. i = 0 arr.each do |k| i += k end i end def arr (depth, min=1, max=10-depth,t=[], arr=[]) (min..max).each do |i| t[depth-1] = i if depth > 0 arr(depth-1, i+1, max+1, t, arr) if depth > 1 arr << t.reverse.clone if depth == 1 end arr end def calc (number, depth) arri = arr(depth) arri.each do |a| arri.delete_if { |a| sum(a) != number } end arri end # examples calc(24, 3).each do |a| print "#{a} - " end puts calc(24, 4).each do |a| print "#{a} - " end puts # even summands of 45 can be calculated now. It was impossible with the older algorithm. calc(45, 9).each do |a| print "#{a} - " end puts