1. 1 Jeremy Henty

    “=> 2551 Much better.” D’ya think? What are the chances of
    the sum of even numbers being odd? How about

    irb(main):002:0> (1 .. 100).inject(0) {|sum, n| (n % 2)==0 ? sum + n : sum }
    => 2550

    That’s better!

  2. 2 Matt Williams

    D’oh! Yeah, you’re right; without the 0 as the argument to inject, it uses the first element as the seed.

Leave a Reply