Sha256: 3b598a9244040404090a835b5b1a9aa9295d7c1fec94b9f5a6cc6c9f7f8f488e
Contents?: true
Size: 754 Bytes
Versions: 1
Compression:
Stored size: 754 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 worklist. =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 worklist = Array(2..n) primes = [] while worklist.first < sqrt(n) primes << worklist.first worklist.delete_if { |x| x % primes.last == 0 } end return primes + worklist end # :end :sieve end # SieveLab end # RubyLabs
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubylabs-0.8.0 | lib/sievelab.rb |