Skip to content

Instantly share code, notes, and snippets.

@jmstacey
Created December 26, 2010 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmstacey/755660 to your computer and use it in GitHub Desktop.
Save jmstacey/755660 to your computer and use it in GitHub Desktop.
Testing concurrent threading in the Rubinius hydra branch
# 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