Solution (source code)

= Solution

The <divergence theorem> states that, for a bounded region $V$ with piecewise smooth boundary $S$, outward unit <normal vector> $n$, and a continuously differentiable <vector field> $F$ on a neighborhood of $\overline V$,
$$
\int_S F\cdot n\,dS=\int_V\nabla\cdot F\,dV.
$$
The outward orientation fixes the sign of the <flux>.

On the <sphere> of radius $R$, the outward unit <normal vector> is $\mathbf r/R$. Thus $r^n\mathbf r\cdot n=R^{n+1}$. Multiplying by the <surface area> $4\pi R^2$ gives
$$
\boxed{I=4\pi R^{n+3}.}
$$
For the second calculation, use the <product rule> in <Cartesian coordinates>:
$$
\nabla\cdot(r^n\mathbf r)=3r^n+\mathbf r\cdot\nabla r^n=(n+3)r^n\qquad(r>0).
$$
The field remains continuously differentiable at the origin when $n>0$. Indeed, for $r>0$ its <Jacobian matrix> entries are $r^n\delta_{ij}+nr^{n-2}x_ix_j$, all tending to zero as $r\to0$; its derivative at zero is also zero because $|r^n\mathbf r|/r=r^n\to0$. The <divergence theorem> therefore applies to the entire ball. In <spherical coordinates>, it yields
$$
I=4\pi(n+3)\int_0^R r^{n+2}\,dr=4\pi R^{n+3},
$$
in agreement with the direct <surface integral>.