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 Response to “Prime numbers, Ruby-style”

  1. Finnbarr Murphy Says:

    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)).

Leave a Reply


http://www.canakkaleruhu.org http://www.vergimevzuati.org http://www.finansaldenetci.com http://www.securityweb.org http://www.siyamiozkan.org http://www.fatmaozkan.com http://www.sgk.biz.tr http://www.denetci.gen.tr http://www.bagimsizdenetim.biz.tr http://www.mevzuat.biz.tr http://www.security.biz.tr http://www.sorgulatr.com http://www.kanunlar.biz http://www.prsorgu.net http://www.sirabul.com http://www.emekliol.org http://www.coklupagerank.com http://www.coklupagerank.net http://www.coklupagerank.org http://www.prsorgu.org http://www.scriptencode.com http://www.sirabul.net http://www.sirabul.org http://www.sitenizanaliz.com http://www.seoisko.com http://www.seomavi.com http://www.scriptencode.net http://www.scriptencode.org