In [1]:
from sympy import *
from sympy import Integral, S, cos, sqrt
from IPython.display import display

In [2]:
init_printing()

In [3]:
x, h, r, t = symbols("x h r t")

# "volume" of a 0 dimensional sphere is 1, i.e. r^0
f0 = r ** 0

def next(f):
x = r * sin(t)
i = Integral(f.subs(r, x), (h, 0, r))
# Rearranging h = r * cos(t) gives t = acos(h / r)
# [transform] does u-substitution for integrals
return 2 * i.transform(acos(h / r), t).doit()

def pretty_print(f, i):
print(f'"Volume" of a {i}-dimensional sphere:')
display(f)

f = f0
for i in range(10):
pretty_print(f, i)
f = next(f)

"Volume" of a 0-dimensional sphere:

$\displaystyle 1$
"Volume" of a 1-dimensional sphere:

$\displaystyle 2 r$
"Volume" of a 2-dimensional sphere:

$\displaystyle \pi r^{2}$
"Volume" of a 3-dimensional sphere:

$\displaystyle \frac{4 \pi r^{3}}{3}$
"Volume" of a 4-dimensional sphere:

$\displaystyle \frac{\pi^{2} r^{4}}{2}$
"Volume" of a 5-dimensional sphere:

$\displaystyle \frac{8 \pi^{2} r^{5}}{15}$
"Volume" of a 6-dimensional sphere:

$\displaystyle \frac{\pi^{3} r^{6}}{6}$
"Volume" of a 7-dimensional sphere:

$\displaystyle \frac{16 \pi^{3} r^{7}}{105}$
"Volume" of a 8-dimensional sphere:

$\displaystyle \frac{\pi^{4} r^{8}}{24}$
"Volume" of a 9-dimensional sphere:

$\displaystyle \frac{32 \pi^{4} r^{9}}{945}$