You compute the parameters for the volume of an \$ n \$-dimensional ball recursively, using the recurrence relation $$ V_1(R) = 2R ,円 , ,円 V_2(R) = \pi R^2 ,円 , \\ V_n(R) = \frac{2 \pi}{n} R^2 \cdot V_{n-2}(R) \text{ for $n \ge 3$.} $$
An alternative would be to compute this iteratively, using the general formula
$$
V_n(R) = \frac{\pi^{n/2}}{\Gamma(n/2+1)} R^n
$$
which is
$$
V_n(R) = \frac{\pi^{n/2}}{(n/2)!} R^n
$$
if \$ n \$ is even, and
$$
V_n(R) = \frac{\pi^{(n-1)/2} 2^{(n+1)/2}}{1 \cdot 3 \cdot 5 \cdots n} R^n
$$
if \$ n \$ is odd. The numerator and denominator are now relative prime so that the gcd
function is not needed anymore, and the constructor would be
Volume(int n) {
this.n = n;
this.piExponent = n / 2;
this.numerator = 1;
this.denominator = 1;
if (n % 2 == 0) {
for (int k = 1; k <= n/2; k++) {
this.denominator *= k;
}
} else {
for (int k = 1; k <= n; k += 2) {
this.numerator *= 2;
this.denominator *= k;
}
}
}
No temporary Volume
objects are created. The getVolume
method is no longer needed and
Volume v = new Volume(n);
is called is called instead. (And I would still prefer the variable name "dimension" over "n".)
Some remarks about the generated \$ \LaTeX \$ code:
- There must be a space between
\pi
andR
if\pi
is not followed by an exponent, otherwise invalid code\piR
is generated. - The last
&
in each line is not necessary. - "Aligning" the \$ \LaTeX \$ code does not work for dimensions \$ n \ge 11 \$ because the volume strings do not fit into 30 characters anymore. The denominators grow quickly! But fixing that is probably not worth the hassle because the alignment only makes the emitted code a bit easier to read, but does not affect the final output.
You compute the parameters for the volume of an \$ n \$-dimensional ball recursively, using the recurrence relation $$ V_1(R) = 2R ,円 , ,円 V_2(R) = \pi R^2 ,円 , \\ V_n(R) = \frac{2 \pi}{n} R^2 \cdot V_{n-2}(R) \text{ for $n \ge 3$.} $$
An alternative would be to compute this iteratively, using the general formula
$$
V_n(R) = \frac{\pi^{n/2}}{\Gamma(n/2+1)} R^n
$$
which is
$$
V_n(R) = \frac{\pi^{n/2}}{(n/2)!} R^n
$$
if \$ n \$ is even, and
$$
V_n(R) = \frac{\pi^{(n-1)/2} 2^{(n+1)/2}}{1 \cdot 3 \cdot 5 \cdots n} R^n
$$
if \$ n \$ is odd. The numerator and denominator are now relative prime so that the gcd
function is not needed anymore, and the constructor would be
Volume(int n) {
this.n = n;
this.piExponent = n / 2;
this.numerator = 1;
this.denominator = 1;
if (n % 2 == 0) {
for (int k = 1; k <= n/2; k++) {
this.denominator *= k;
}
} else {
for (int k = 1; k <= n; k += 2) {
this.numerator *= 2;
this.denominator *= k;
}
}
}
The getVolume
method is no longer needed and
Volume v = new Volume(n);
is called is called instead. (And I would still prefer the variable name "dimension" over "n".)
Some remarks about the generated \$ \LaTeX \$ code:
- There must be a space between
\pi
andR
if\pi
is not followed by an exponent, otherwise invalid code\piR
is generated. - The last
&
in each line is not necessary. - "Aligning" the \$ \LaTeX \$ code does not work for dimensions \$ n \ge 11 \$ because the volume strings do not fit into 30 characters anymore. The denominators grow quickly! But fixing that is probably not worth the hassle because the alignment only makes the emitted code a bit easier to read, but does not affect the final output.
You compute the parameters for the volume of an \$ n \$-dimensional ball recursively, using the recurrence relation $$ V_1(R) = 2R ,円 , ,円 V_2(R) = \pi R^2 ,円 , \\ V_n(R) = \frac{2 \pi}{n} R^2 \cdot V_{n-2}(R) \text{ for $n \ge 3$.} $$
An alternative would be to compute this iteratively, using the general formula
$$
V_n(R) = \frac{\pi^{n/2}}{\Gamma(n/2+1)} R^n
$$
which is
$$
V_n(R) = \frac{\pi^{n/2}}{(n/2)!} R^n
$$
if \$ n \$ is even, and
$$
V_n(R) = \frac{\pi^{(n-1)/2} 2^{(n+1)/2}}{1 \cdot 3 \cdot 5 \cdots n} R^n
$$
if \$ n \$ is odd. The numerator and denominator are now relative prime so that the gcd
function is not needed anymore, and the constructor would be
Volume(int n) {
this.n = n;
this.piExponent = n / 2;
this.numerator = 1;
this.denominator = 1;
if (n % 2 == 0) {
for (int k = 1; k <= n/2; k++) {
this.denominator *= k;
}
} else {
for (int k = 1; k <= n; k += 2) {
this.numerator *= 2;
this.denominator *= k;
}
}
}
No temporary Volume
objects are created. The getVolume
method is no longer needed and
Volume v = new Volume(n);
is called instead. (And I would still prefer the variable name "dimension" over "n".)
Some remarks about the generated \$ \LaTeX \$ code:
- There must be a space between
\pi
andR
if\pi
is not followed by an exponent, otherwise invalid code\piR
is generated. - The last
&
in each line is not necessary. - "Aligning" the \$ \LaTeX \$ code does not work for dimensions \$ n \ge 11 \$ because the volume strings do not fit into 30 characters anymore. The denominators grow quickly! But fixing that is probably not worth the hassle because the alignment only makes the emitted code a bit easier to read, but does not affect the final output.
You compute the parameters for the volume of an \$ n \$-dimensional ball recursively, using the recurrence relation $$ V_1(R) = 2R ,円 , ,円 V_2(R) = \pi R^2 ,円 , \\ V_n(R) = \frac{2 \pi}{n} R^2 \cdot V_{n-2}(R) \text{ for $n \ge 3$.} $$
An alternative would be to compute this iteratively, using the general formula
$$
V_n(R) = \frac{\pi^{n/2}}{\Gamma(n/2+1)} R^n
$$
which is
$$
V_n(R) = \frac{\pi^{n/2}}{(n/2)!} R^n
$$
if \$ n \$ is even, and
$$
V_n(R) = \frac{\pi^{(n-1)/2} 2^{(n+1)/2}}{1 \cdot 3 \cdot 5 \cdots n} R^n
$$
if \$ n \$ is odd. The numerator and denominator are now relative prime so that the gcd
function is not needed anymore, and the constructor would be
Volume(int n) {
this.n = n;
this.piExponent = n / 2;
this.numerator = 1;
this.denominator = 1;
if (n % 2 == 0) {
for (int k = 1; k <= n/2; k++) {
this.denominator *= k;
}
} else {
for (int k = 1; k <= n; k += 2) {
this.numerator *= 2;
this.denominator *= k;
}
}
}
The getVolume
method is no longer needed and
Volume v = new Volume(n);
is called is called instead. (And I would still prefer the variable name "dimension" over "n".)
Some remarks about the generated \$ \LaTeX \$ code:
- There must be a space between
\pi
andR
if\pi
is not followed by an exponent, otherwise invalid code\piR
is generated. - The last
&
in each line is not necessary. - "Aligning" the \$ \LaTeX \$ code does not work for dimensions \$ n \ge 11 \$ because the volume strings do not fit into 30 characters anymore. The denominators grow quickly! But fixing that is probably not worth the hassle because the alignment only makes the emitted code a bit easier to read, but does not affect the final output.