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/README.rdoc +15 -6
- data/Rakefile +3 -0
- data/VERSION +1 -1
- data/lib/bitlab.rb +593 -328
- data/lib/demos.rb +20 -9
- data/lib/elizalab.rb +660 -507
- data/lib/hashlab.rb +289 -192
- data/lib/introlab.rb +33 -38
- data/lib/iterationlab.rb +117 -61
- data/lib/marslab.rb +608 -475
- data/lib/randomlab.rb +227 -121
- data/lib/recursionlab.rb +197 -140
- data/lib/rubylabs.rb +936 -390
- data/lib/sievelab.rb +32 -24
- data/lib/spherelab.rb +308 -220
- data/lib/tsplab.rb +634 -312
- data/test/bit_test.rb +4 -4
- data/test/tsp_test.rb +18 -0
- metadata +2 -2
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
|
-
|
7
|
-
|
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
|
14
|
-
|
15
|
-
|
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
|
-
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|