Last active
July 6, 2025 15:01
-
-
Save coderodde/47e308e403a52ffccb179c03c70fae61 to your computer and use it in GitHub Desktop.
A small Java program for computing volumes of n-dimensional balls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.coderodde.math.simulation.volumes; | |
public class NdimensionalBallVolumes { | |
private static final int LARGEST_N = 10; | |
public static void main(String[] args) { | |
System.out.println("$$"); | |
System.out.println("\\begin{aligned}"); | |
int maxLineNumberLength = Integer.toString(LARGEST_N).length(); | |
String lineFormat = | |
String.format( | |
"V_{%%%dd} &= %%-30s & \\\\ %%%% n = %%%dd\n", | |
maxLineNumberLength, | |
maxLineNumberLength); | |
for (int n = 1; n <= LARGEST_N; ++n) { | |
Volume v = getVolume(n); | |
System.out.printf(lineFormat, | |
n, | |
v.toString(), | |
n); | |
} | |
System.out.println("\\end{aligned}"); | |
System.out.println("$$"); | |
} | |
static Volume getVolume(int n) { | |
if (n == 1) { | |
return new Volume(2, 1, 0, n); | |
} | |
if (n == 2) { | |
return new Volume(1, 1, 1, n); | |
} | |
Volume previous = getVolume(n - 2); | |
return new Volume(previous.numerator * 2, | |
previous.denominator * n, | |
previous.piExponent + 1, | |
n); | |
} | |
static final class Volume { | |
long numerator; | |
long denominator; | |
int piExponent; | |
int n; | |
Volume(long numerator, | |
long denominator, | |
int piExponent, | |
int n) { | |
long gcd = gcd(numerator, | |
denominator); | |
this.numerator = numerator / gcd; | |
this.denominator = denominator / gcd; | |
this.piExponent = piExponent; | |
this.n = n; | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
if (denominator == 1) { | |
appendNumeratorString(sb); | |
} else { | |
sb.append("\\frac{"); | |
appendNumeratorString(sb); | |
sb.append("}{"); | |
sb.append(denominator); | |
sb.append("}"); | |
} | |
return sb.toString(); | |
} | |
private void appendNumeratorString(StringBuilder sb) { | |
if (numerator > 1) { | |
sb.append(numerator) | |
.append(" "); | |
} | |
if (piExponent > 0) { | |
sb.append("\\pi"); | |
if (piExponent > 1) { | |
sb.append("^{") | |
.append(piExponent) | |
.append("}"); | |
} | |
} | |
sb.append(n == 1 ? "R" : " R"); | |
if (n > 1) { | |
sb.append("^{") | |
.append(n) | |
.append("}"); | |
} | |
} | |
} | |
static long gcd(long a, long b) { | |
while (b != 0) { | |
long t = b; | |
b = a % b; | |
a = t; | |
} | |
return a; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.coderodde.math.simulation.volumes; | |
import io.github.coderodde.math.simulation.volumes.NdimensionalBallVolumes.Volume; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class NdimensionalBallVolumesTest { | |
@Test | |
public void n1() { | |
Volume f = NdimensionalBallVolumes.getVolume(1); | |
assertEquals(2, f.numerator); | |
assertEquals(1, f.denominator); | |
assertEquals(0, f.pis); | |
String s = f.toString(); | |
System.out.println(s); | |
} | |
@Test | |
public void n2() { | |
Volume f = NdimensionalBallVolumes.getVolume(2); | |
assertEquals(1, f.numerator); | |
assertEquals(1, f.denominator); | |
assertEquals(1, f.pis); | |
String s = f.toString(); | |
System.out.println(s); | |
} | |
@Test | |
public void n4() { | |
Volume f = NdimensionalBallVolumes.getVolume(4); | |
assertEquals(1, f.numerator); | |
assertEquals(2, f.denominator); | |
assertEquals(2, f.pis); | |
String s = f.toString(); | |
System.out.println(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment