Created
December 27, 2010 07:05
-
-
Save jmstacey/755928 to your computer and use it in GitHub Desktop.
Includes output from each thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lucas–Lehmer primality test | |
# Retrieved from http://rosettacode.org/wiki/Lucas-Lehmer_test | |
def is_prime?(p) | |
if p == 2 | |
return true | |
elsif p <= 1 || p % 2 == 0 | |
return false | |
else | |
(3 .. Math.sqrt(p)).step(2) do |i| | |
if p % i == 0 | |
return false | |
end | |
end | |
return true | |
end | |
end | |
def generate_primes(low, high, thread) | |
puts "Generating primes between #{low} and #{high}" | |
primes = (low..high).to_a | |
primes.size.times do |i| | |
unless is_prime?(primes[i]) | |
primes[i] = nil | |
else | |
puts "primes.rb thread #{thread} says #{primes[i]} is prime" | |
end | |
end | |
primes.compact | |
end | |
t1 = Thread.new { p generate_primes(0, 100000, "t1") } | |
t2 = Thread.new { p generate_primes(100001, 200000, "t2") } | |
t1.join | |
t2.join | |
puts "All done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment