```
func countPrimes(_ n: Int) -> Int {
if n <= 2 { return 0 }
// var odd numbers are prime , even numbers are not.
var notPrime:[Bool] = []
for i in 0..<n{
if i%2 == 0 { notPrime.append(true) }
else {notPrime.append(false) }
}
// start with 3 and plus 2 each time
var count = 1 // including 2
var num = 3
while num < n{
if notPrime[num] == false{
count += 1
var j = num
while j*num < n{
notPrime[num*j] = true
j+=2
}
}
num += 2
}
return count
}
```