William James wrote: > Daniel Baird wrote: > > On 8/2/06, Daniel Baird <danielbaird / gmail.com> wrote: > > > Hi all, > > > > > > I've been golfing around with the sieve of Eratosthenes. Here's what > > > I've got so far: > > > > > > a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact > > > > > > It's already under 80 chars, but I'd still love to remove the > > > definition of the array a, and do the whole thing with no semicolons. > > > Any suggestions? > > > > > > > Improvement: > > > > a=(2..100).to_a;p a.each{|c|a.reject!{|d|c<d&&d%c==0}} > > > > .swapped to reject, and now I don't have to test for c and d being > > nil, or do the final compact. Seems ok even though I'm editing the > > array I'm looping through.. > > p (2..100).inject([]){|a,n|a.any?{|i|n%i==0}?a:a<<n} p (2..100).inject([]){|a,n|a.any?{|i|n%i<1}?a:a<<n}