Rewrote #prime? to be much faster.

This new version executes an order of magnitude faster.

    require 'benchmark'
    require 'prime'

    def prime?(n)
      return true if n == 2
      return false if n % 2 == 0 || n < 2
      (3..(n**0.5).floor).step(2) { |i|
        return false if n % i == 0
      }
      true
    end

    # Current Method
    puts Benchmark.measure {
      (1..1_000_000).each { |i| i.prime? }
    }

    # Improved
    puts Benchmark.measure {
      # Current Method
      (1..1_000_000).each { |i| prime?(i) }
    }

    # Current Method
    puts Benchmark.measure {
      1000.times {
        (1..1_000).each { |i| i.prime? }
      }
    }

    # Improved
    puts Benchmark.measure {
      1000.times {
        (1..1_000).each { |i| prime?(i) }
      }
    }




It might be more readable, but not really faster (for Fixnum). That's because % and == are optimized to not use method dispatch if possible.

My gem doesn't find a statistically significant difference:

    require 'fruity'
    compare do
      modulo { 42 % 2 == 0 }
      even   { 42.even? }
    end
    # => even is similar to modulo