Sha256: 5fd695a2f8326633253651449df2f31202b4a1ac88d4e0a24e72c81f38fdf47e

Contents?: true

Size: 760 Bytes

Versions: 1

Compression:

Stored size: 760 Bytes

Contents

=begin rdoc
  
== Sieve of Eratosthenes

Use the Sieve of Eratosthenes algorithm to generate a list of prime
numbers.  The method is an introduction to iteration, using iterators
to make and filter lists of numbers, and a while loop to repeat the
filtering step until no more composite numbers are left in the worksheet.

=end

module RubyLabs
	
module SieveLab

=begin rdoc
  Call +sieve(n)+ to create an array of prime numbers between +2+ and +n+  
=end

# :begin :sieve
	def sieve(n)
	  return [] if n < 2
	  worksheet = Array(2..n)
		primes = []
	
	  while worksheet.first < sqrt(n)
	    primes << worksheet.first
	    worksheet.delete_if { |x| x % primes.last == 0 }
	  end

		return primes + worksheet
	end
# :end :sieve

end # SieveLab

end # RubyLabs

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubylabs-0.8.1 lib/sievelab.rb