rubylabs 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sievelab.rb CHANGED
@@ -1,35 +1,43 @@
1
-
1
+ module RubyLabs
2
+
2
3
  =begin rdoc
3
4
 
4
5
  == Sieve of Eratosthenes
5
6
 
6
- Use the Sieve of Eratosthenes algorithm to generate a list of prime
7
- numbers. The method is an introduction to iteration, using iterators
8
- to make and filter lists of numbers, and a while loop to repeat the
9
- filtering step until no more composite numbers are left in the worksheet.
10
-
11
- =end
7
+ The SieveLab module contains the Ruby code described in Chapter 3
8
+ of <em>Explorations in Computing</em>.
12
9
 
13
- module RubyLabs
14
-
15
- module SieveLab
10
+ The module has only one method, +sieve+, which uses the Sieve of Eratosthenes
11
+ to generate a list of prime number up to a specified maximum value.
12
+ The method is an introduction to iteration, using iterators
13
+ to make and filter lists of numbers.
16
14
 
17
- =begin rdoc
18
- Call +sieve(n)+ to create an array of prime numbers between +2+ and +n+
19
15
  =end
16
+
17
+ module SieveLab
20
18
 
21
- # :begin :sieve
22
- def sieve(n)
23
- worksheet = Array(2..n)
24
- primes = []
25
-
26
- while worksheet.first < sqrt(n)
27
- primes << worksheet.first
28
- worksheet.delete_if { |x| x % primes.last == 0 }
29
- end
30
-
31
- return primes + worksheet
32
- end
19
+ # Create a list of all prime numbers between 2 and +n+.
20
+ #
21
+ # Example:
22
+ # >> sieve(1000)
23
+ # => [2, 3, 5, 7, ... 983, 991, 997]
24
+ #
25
+ # :call-seq:
26
+ # sieve(n) => Array
27
+ #
28
+ #--
29
+ # :begin :sieve
30
+ def sieve(n)
31
+ worksheet = Array(2..n)
32
+ primes = []
33
+
34
+ while worksheet.first < sqrt(n)
35
+ primes << worksheet.first
36
+ worksheet.delete_if { |x| x % primes.last == 0 }
37
+ end
38
+
39
+ return primes + worksheet
40
+ end
33
41
  # :end :sieve
34
42
 
35
43
  end # SieveLab