Solution (source code)

= Solution

The <sphere> of centre $\mathbf a$ and radius $p$ has equation
$$
\boxed{|\mathbf r-\mathbf a|^2=p^2}.
$$
Let $d=|\mathbf b-\mathbf a|$ be the distance between the centres. A nondegenerate <circle of intersection of two spheres> exists exactly when
$$
\boxed{|p-q|<d<p+q}.
$$
The upper inequality excludes disjoint or externally tangent <spheres>. The lower excludes strict containment and internal tangency. It also excludes coincident centres, which give either no intersection or an entire common <sphere> rather than one <circle>. These inequalities are sufficient because a triangle with side lengths $p,q,d$ then exists with nonzero area, and rotating its third vertex about the line of centres traces a <circle>.

For a point on both <spheres>, expand their equations:
$$
\mathbf r^2-2\mathbf a\cdot\mathbf r+a^2=p^2,\qquad
\mathbf r^2-2\mathbf b\cdot\mathbf r+b^2=q^2.
$$
Subtracting cancels $\mathbf r^2$ and gives the <plane>
$$
\boxed{2(\mathbf b-\mathbf a)\cdot\mathbf r=p^2-q^2+b^2-a^2}.
$$
Its normal is parallel to the line of centres. Under the strict inequalities, the <plane> cuts either <sphere> in a genuine <circle>.