Sha256: 4917e196826a18b870357b509ee6e6025dcc7732ffdfe61efb7a3fe131d02ab2
Contents?: true
Size: 738 Bytes
Versions: 3
Compression:
Stored size: 738 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) 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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rubylabs-0.9.0 | lib/sievelab.rb |
rubylabs-0.8.3 | lib/sievelab.rb |
rubylabs-0.8.2 | lib/sievelab.rb |