### These formulae come from applying basic geometry

In the exterior case, the supplement of this angle along the original radius of the large circle extended through the centre of the small circle measures the total rotation of the small circle, and its opposite angles are the angles swept out by the original tangent points in each circle. So the rotation is π - (π - (d/r + d/R)) = d(1/r + 1/R), where d is the partial circumference swept and r, R are the radii of the smaller and larger circles.
In the interior case, this angle actually measures the total rotation of the small circle. Its opposite angles are the angle swept out by the original tangent point in the larger circle and the supplement of the angle swept out by the original tangent point in the smaller circle. So the rotation is π - ((π - d/r) + d/R)) = d(1/r - 1/R).
When the whole of circumference of the larger circle has been rolled, the total rotation of the smaller circle is 2πR(1/r +/- 1/R) = 2π(1 +/- R/r). So, in each case the problem is soluble if r is a divisor of R, and the number of rotations is the integer 1 + R/r for exterior rolling, or 1 - R/r for interior: in this example, 5 or 3.