204 LeetCode Java: Count Primes – Easy¶
Problem:¶
Count the number of prime numbers less than a non-negative number, n.
Thoughts:¶
Because for this problem, it is not simple check if one number is prime or not, we could not use the trivial way to check. Otherwise it would be too slow.
The solution below is using Sieve of Eratosthenes way to calculate composite.
All numbers under n that is not a composite, it is a prime. Of course 1 is not a prime.