Solution (source code)

= Solution

For $r=\sqrt{x^2+y^2+z^2}>0$, the <radial vector field> has components $F_i=x_if(r)$. The <product rule> gives
$$
\nabla\cdot\mathbf F=\sum_i\left[f(r)+x_if'(r)\frac{x_i}{r}\right]=3f(r)+rf'(r).
$$
Thus for $f(r)=r^{n-1}$,
$$
\boxed{\nabla\cdot\mathbf F=(n+2)r^{n-1},\qquad r>0}.
$$
When $n=-2$, $\mathbf F=\hat{\mathbf r}/r^2$ and the ordinary <divergence> is zero at every nonzero radius. However, the field is singular at the origin. Its outward <flux> through a radius-$R$ sphere is $(1/R^2)(4\pi R^2)=4\pi$, so one cannot apply the smooth-field <divergence theorem> across the origin and conclude that this flux vanishes.

In the language of <distributions>, the complete <distributional divergence> identity is $\boxed{\nabla\cdot(\hat{\mathbf r}/r^2)=4\pi\delta^{(3)}(\mathbf r)}$. Here $\delta^{(3)}$ is the three-dimensional <Dirac delta function>. Without that generalized interpretation, the correct statement is simply zero divergence away from the origin and undefined ordinary divergence at the singular point. Removing a small ball around the origin restores the ordinary theorem: the inner boundary has inward radial orientation and cancels the outer flux.