On Jan 17, 2007, at 10:10 PM, William James wrote:

> Comparing Ruby to Lua and LuaJIT for intensively computing primes:
>
> Ruby   245.763 seconds
> Lua     10.685 seconds
> LuaJIT   1.311 seconds
>
> # the Ruby program
> def prime(n)
>   if n > 2 and n % 2 == 0
>     return false
>   end
>   3.step( Math.sqrt(n).floor, 2){|i|
>     if n % i == 0
>       return false
>     end
>   }
>   true
> end

I wrote mine a bit different:

   def prime(n)
     return false if n > 2 and n % 2 == 0
     max = sqrt(n)
     3.upto(max) do |i|
       return false if i % 2 != 0 and n % i == 0
     end
     return true
   end

   optimize :prime

---

now, optimize converts it to C which doesn't have the benefits of big  
numerics built in, but that doesn't sound like a problem for the OP.  
Here are my times:

pure ruby: real 0m1.139s user 0m1.110s sys 0m0.009s
opt  ruby: real 0m0.259s user 0m0.201s sys 0m0.056s

You can stay in ruby-land and eat your cake too.