'Time Limit Exceeded' for my ruby solution

  • 0
    def count_primes(n)
      return 0 if n < 2
      hash = {}
      for i in (2..n-1)
        next if hash[i] == 0
        (2*i..n-1).step(i).each {|j| hash[j] = 0}
      return n - hash.values.length - 2

    I have tried several ways, but it still take too much time and report 'Last executed input: 999983'.

Log in to reply

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