Created
December 26, 2010 21:36
-
-
Save jmstacey/755660 to your computer and use it in GitHub Desktop.
Testing concurrent threading in the Rubinius hydra branch
This file contains hidden or 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) | |
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 | |
end | |
end | |
primes.compact | |
end | |
t1 = Thread.new { primes1 = generate_primes(0, 10000) } | |
t2 = Thread.new { primes2 = generate_primes(10001, 20000) } | |
t1.join | |
t2.join | |
puts "All done!" | |
# exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment