Ruby 4 lines


  • 0
    L
    def count_primes(n)
        return 0 if n < 2
        a = Array.new(n, true)
        (2..Math.sqrt(n).to_i).each {|i| (2*i...n).step(i).each {|j| a[j] = false} if a[i] }
        return a[2..-1].inject(0){|sum, x| sum + (x ? 1 : 0)}
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.