Solution (source code)

= Solution

In the <long-solenoid approximation>, the internal <magnetic flux density> is uniform, $B=\mu_0nI$, and the external <magnetic field> is negligible. This follows from the <magnetostatic Ampère law> applied to a rectangular loop running inside and outside the <solenoid>. Its number of turns is $N=nD$ and its cross-sectional <area> is $A=\pi a^2$. Each turn links <magnetic flux> $\Phi_B=BA=\mu_0nI\pi a^2$. Therefore the <flux linkage> is
$$
\Lambda=N\Phi_B=\mu_0n^2D\pi a^2I.
$$
Using the definition of <self-inductance>, $\Lambda=LI$, gives
$$
\boxed{L=\mu_0n^2\pi a^2D.}
$$
This neglects end leakage and winding thickness and uses <vacuum permeability> for the air core.