-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_parallelism.rb
55 lines (47 loc) · 1 KB
/
result_parallelism.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'rinda/tuplespace'
require 'drb'
require 'rinda_eval'
require 'benchmark'
DRb.start_service
$ts = Rinda::TupleSpace.new
def is_prime(candidate)
limit = Math.sqrt(candidate)
(2..limit).each do |i|
un1, un2, ok = $ts.read([:primes, i, nil])
return false if ok && (candidate % i == 0)
end
true
end
class Primes
def initialize(limit)
@limit = limit
end
def generate
(2..@limit).each do |each|
$ts.rinda_eval { [:primes, each, is_prime(each)] }
end
end
def count
result = 0
(2..@limit).each do |each|
_, _, is_prime = $ts.read([:primes, each, nil])
result += 1 if is_prime
end
result
end
def print
primes = (2..@limit).select do |each|
_, _, is_prime = $ts.read([:primes, each, nil])
is_prime
end
primes.each {|prime| puts "#{prime} is prime" }
end
end
primes = Primes.new(500)
count = 0
puts Benchmark.measure {
primes.generate
count = primes.count
}
raise "Wrong was #{count}" if count != 95
primes.print