On Sep 4, 11:40 pm, RichardOnRails
<RichardDummyMailbox58... / uscomputergurus.com> wrote:
> Hi All,
>
> My middle-school granddaughter posed the following challenge to me:
>
> 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 athttp://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√‘ in
> there
> intended to give her a sense of the programs functionality.
>
> Any ideas/suggestions?
>
> Thanks in Advance,
> Richard

min = 1_000_000
answer = nil
"34567".upto("76543"){|s|
  next  if s !~ /^[3-7]+$/
  next  if s.split("").uniq.size < 5
  m1,m2 = s[0,3].to_i, s[3,2].to_i
  if m1 * m2 < min
    min = m1 * m2
    answer = m1, m2
  end
}
p answer


p ("34567".."76543").select{|s|
  s =~ /^[3-7]+$/  and  s.split("").uniq.size == 5 }.
  sort_by{|s| s[0,3].to_i * s[3,2].to_i }.first