On 6/18/07, Eric I. <rubytraining / gmail.com> wrote:
> On Jun 18, 3:40 pm, "Gregory Brown" <gregory.t.br... / gmail.com> wrote:
> > Wouldn't it be:
> >
> > def fact(n); n > 1 ? n + fact(n-1) : 1; end
> > puts (1..12).inject(0) { |s,r| s + fact(r) }
>
> That certainly works, although I'm personally uncomfortable with the
> name of your recursive method; "fact" seems to imply factorial, which
> this is not.

Dur. You're right.  12! = 12*11*10*..

/me is a math major, too.

> If you like inject, how about a pair of 'em:
>
> puts (1..12).inject(0) { |sum, day|
>   sum + (1..day).inject { |day_sum, gifts| day_sum + gifts }
> }

yup, that looks pretty good.