```
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}
end
return n - hash.values.length - 2
end
```

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