From: "Peña, Botp" Date: 2008-09-05T14:20:30+09:00 Subject: Re: Need a 40 LOC (ignoring comments) to be shorter -- suggestions wanted From: RichardOnRails # My middle-school granddaughter posed the following challenge to me: cool daughter. me, only sons, very terrible when they fight each other :)) # Within 10 minutes, Use the digits 3 to 7 in any order # to form a three-digit multiplicand and a two-digit multiplier # such that their product is minimal. # Out of curiosity as to whether my guess was right, # and with a view toward eliciting an interest in her # for Ruby programming, I wrote the program posted at # http://www.pastie.org/266447 # The program reveals that I was wrong, but it took me about 40 lines of # code, ignoring comments. That would probably overwhelm her. # I was hoping that the code could be condensed with Ruby-isms. # BTW, I left some put’s in there # intended to give her a sense of the programs functionality. my try, botp@botp-desktop:~$ cat test.rb digits = [3,4,5,6,7] min_product=1/0.0 min_multiplicand = 0 min_multiplier = 0 digits.permutation(3).each do |multiplicand_digits| h,t,ones = multiplicand_digits multiplicand = h*100 + t*10 + ones (digits-multiplicand_digits).permutation(2).each do |multiplier_digits| t,ones = multiplier_digits multiplier = t*10 + ones prod = multiplicand * multiplier if prod < min_product min_product = prod min_multiplicand = multiplicand min_multiplier = multiplier end end end puts "#{min_multiplicand} * #{min_multiplier} = #{min_product}" botp@botp-desktop:~$ ruby test.rb 467 * 35 = 16345 is that ok? kind regards -botp