When it comes to finding prime numbers, the Sieve of Eratosthenes and the Sieve of Atkin are two possible solutions. The sieve of Eratosthenes has complexity O ((n log n) (log log n)). Atkin sieve has O (N / log log n) complexity.
If you have a number and want to know if it is simple, this is called running a primality test . The naive approach is to check all numbers m from 2 to sqrt (n) and check that n% m is not 0. If you want to expand this a bit, you can throw out all the even numbers (except 2). There are also some other improvements to this naive approach that can improve performance, as well as other, more advanced methods.
source
share