  In this paper, we present a fast divergence-free spectral algorithm (FDSA)
for the curl-curl problem. Divergence-free bases in two and three dimensions
are constructed by using the generalized Jacobi polynomials. An accurate
spectral method with exact preservation of the divergence-free constraint
point-wisely is then proposed, and its corresponding error estimate is
established. We then present a highly efficient solution algorithm based on a
combination of matrix-free preconditioned Krylov subspace iterative method and
a fully diagonalizable auxiliary problem, which is derived from the spectral
discretisations of generalized eigenvalue problems of Laplace and biharmonic
operators. We rigorously prove that the dimensions of the invariant subspace of
the preconditioned linear system resulting from the divergence-free spectral
method with respect to the dominate eigenvalue $1$, are $(N-3)^2$ and
$2(N-3)^3$ for two- and three-dimensional problems with $(N-1)^2$ and
$2(N-1)^3$ unknowns, respectively. Thus, the proposed method usually takes only
several iterations to converge, and astonishingly, as the problem size
(polynomial order) increases, the number of iterations will decrease, even for
highly indefinite system and oscillatory solutions. As a result, the
computational cost of the solution algorithm is only a small multiple of $N^3$
and $N^4$ floating number operations for 2D and 3D problems, respectively.
Plenty of numerical examples for solving the curl-curl problem with both
constant and variable coefficients in two and three dimensions are presented to
demonstrate the accuracy and efficiency of the proposed method.
