In the trial division algorithm, the most work that may be needed to determine whether a number n
is prime, is testing divisibility by the primes up to about sqrt(n)
.
That worst case is met when n
is a prime or the product of two primes of nearly the same size (including squares of primes). If n
has more than two prime factors, or two prime factors of very different size, at least one of them is much smaller than sqrt(n)
, so even the accumulated work needed for all these numbers (which form the vast majority of all numbers up to N
, for sufficiently large N
) is relatively insignificant, I shall ignore that and work with the fiction that composite numbers are determined without doing any work (the products of two approximately equal primes are few in number, so although individually they cost as much as a prime of similar size, altogether that's a negligible amount of work).
So, how much work does the testing of the primes up to N
take?
By the prime number theorem, the number of primes <= n
is (for n
sufficiently large), about n/log n
(it's n/log n + lower order terms
). Conversely, that means the k-th prime is (for k not too small) about k*log k
(+ lower order terms).
Hence, testing the k-th prime requires trial division by pi(sqrt(p_k))
, approximately 2*sqrt(k/log k)
, primes. Summing that for k <= pi(N) ~ N/log N
yields roughly 4/3*N^(3/2)/(log N)^2
divisions in total. So by ignoring the composites, we found that finding the primes up to N
by trial division (using only primes), is Omega(N^1.5 / (log N)^2)
. Closer analysis of the composites reveals that it's Theta(N^1.5 / (log N)^2)
. Using a wheel reduces the constant factors, but doesn't change the complexity.
In the sieve, on the other hand, each composite is crossed off as a multiple of at least one prime. Depending on whether you start crossing off at 2*p
or at p*p
, a composite is crossed off as many times as it has distinct prime factors or distinct prime factors <= sqrt(n)
. Since any number has at most one prime factor exceeding sqrt(n)
, the difference isn't so large, it has no influence on complexity, but there are a lot of numbers with only two prime factors (or three with one larger than sqrt(n)
), thus it makes a noticeable difference in running time. Anyhow, a number n > 0
has only few distinct prime factors, a trivial estimate shows that the number of distinct prime factors is bounded by lg n
(base-2 logarithm), so an upper bound for the number of crossings-off the sieve does is N*lg N
.
By counting not how often each composite gets crossed off, but how many multiples of each prime are crossed off, as IVlad already did, one easily finds that the number of crossings-off is in fact Theta(N*log log N)
. Again, using a wheel doesn't change the complexity but reduces the constant factors. However, here it has a larger influence than for the trial division, so at least skipping the evens should be done (apart from reducing the work, it also reduces storage size, so improves cache locality).
So, even disregarding that division is more expensive than addition and multiplication, we see that the number of operations the sieve requires is much smaller than the number of operations required by trial division (if the limit is not too small).
Summarising:
Trial division does futile work by dividing primes, the sieve does futile work by repeatedly crossing off composites. There are relatively few primes, but many composites, so one might be tempted to think trial division wastes less work.
But: Composites have only few distinct prime factors, while there are many primes below sqrt(p)
.