Prime numbers, Ruby-style

Uses the Sieve of Eratosthenes technique, and the intruiging inject() method.
This returns an array of all the primes between 2 and 1000 inclusive.

(2..1000).inject((2..1000).to_a) {|res, i| res.select{|n|n==i||n%i!=0} }

One Comment

  1. This works well for small number range as above. However for a larger number range, say (2..10000000) the time taken is not linear O(n) or even squared O(nn) but is O((nlogn)(loglogn)).