Created
July 14, 2025 09:27
-
-
Save coderodde/4766e05aabe8fb188f7eb360878e7282 to your computer and use it in GitHub Desktop.
A Java program generating the a_n values for rotational energies.
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.physics.mechanics.rotation; | |
| import java.math.BigInteger; | |
| /** | |
| * This class computes some rotational energies of {@code n}-dimensional, solid, | |
| * equidense balls. | |
| * | |
| * @version 1.1.0 (Jul 14, 2025) | |
| * @since 1.0.0 (Jul 13, 2025) | |
| */ | |
| public final class RotationalEnergies { | |
| public static void main(String[] args) { | |
| for (int dimension = 2; dimension <= 10; ++dimension) { | |
| System.out.println(getA(dimension).toString()); | |
| } | |
| } | |
| private static Fraction getA(int n) { | |
| Fraction an = getPiPerN(n).multiply(getV(n).reciprocal()); | |
| for (int i = 1; i <= n - 2; ++i) { | |
| Fraction in = getI(i); | |
| an = an.multiply(in); | |
| } | |
| return an; | |
| } | |
| private static Fraction getPiPerN(int n) { | |
| return new Fraction(BigInteger.ONE, | |
| BigInteger.valueOf(n), | |
| 1, | |
| 0); | |
| } | |
| private static Fraction getV(int n) { | |
| int piExponent = n / 2; | |
| BigInteger numerator = BigInteger.ONE; | |
| BigInteger denominator = BigInteger.ONE; | |
| if (n % 2 == 0) { | |
| for (int k = 1; k <= n / 2; ++k) { | |
| denominator = denominator.multiply(BigInteger.valueOf(k)); | |
| } | |
| } else { | |
| for (int k = 1; k <= n; k += 2) { | |
| numerator = numerator .multiply(BigInteger.valueOf(2)); | |
| denominator = denominator.multiply(BigInteger.valueOf(k)); | |
| } | |
| } | |
| return new Fraction(numerator, | |
| denominator, | |
| piExponent, | |
| 0); | |
| } | |
| private static Fraction getI(int n) { | |
| switch (n) { | |
| case 1: | |
| return new Fraction(BigInteger.ONE, | |
| BigInteger.ONE, | |
| 0, | |
| 0); | |
| case 2: | |
| return new Fraction(BigInteger.ONE, | |
| BigInteger.valueOf(2), | |
| 1, | |
| 0); | |
| default: | |
| Fraction previousFraction = getI(n - 2); | |
| Fraction auxFraction = new Fraction(BigInteger.valueOf(n - 1), | |
| BigInteger.valueOf(n), | |
| 0, | |
| 0); | |
| return auxFraction.multiply(previousFraction); | |
| } | |
| } | |
| } | |
| class Fraction { | |
| private final BigInteger numerator; | |
| private final BigInteger denominator; | |
| private final int piExponentNumerator; | |
| private final int piExponentDenominator; | |
| Fraction(BigInteger numerator, | |
| BigInteger denominator, | |
| int piExponentNumerator, | |
| int piExponentDenominator) { | |
| this.numerator = numerator; | |
| this.denominator = denominator; | |
| int minPiExponent = Math.min(piExponentNumerator, | |
| piExponentDenominator); | |
| this.piExponentNumerator = piExponentNumerator - minPiExponent; | |
| this.piExponentDenominator = piExponentDenominator - minPiExponent; | |
| } | |
| Fraction multiply(Fraction f) { | |
| BigInteger newNumerator = this.numerator .multiply(f.numerator); | |
| BigInteger newDenomitator = this.denominator.multiply(f.denominator); | |
| int newPiExponentNumerator = this.piExponentNumerator | |
| + f.piExponentNumerator; | |
| int newPiExponentDenomirator = this.piExponentDenominator | |
| + f.piExponentDenominator; | |
| return new Fraction(newNumerator, | |
| newDenomitator, | |
| newPiExponentNumerator, | |
| newPiExponentDenomirator); | |
| } | |
| Fraction reciprocal() { | |
| return new Fraction(denominator, | |
| numerator, | |
| piExponentDenominator, | |
| piExponentNumerator); | |
| } | |
| /** | |
| * {@inheritDoc } | |
| */ | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder("\\frac{"); | |
| sb.append(numerator); | |
| if (piExponentNumerator > 0) { | |
| sb.append(" \\pi"); | |
| if (piExponentNumerator > 1) { | |
| sb.append("^{").append(piExponentNumerator).append("}"); | |
| } | |
| } | |
| sb.append("}{").append(denominator); | |
| if (piExponentDenominator > 0) { | |
| sb.append(" \\pi"); | |
| if (piExponentDenominator > 1) { | |
| sb.append("^{").append(piExponentDenominator).append("}"); | |
| } | |
| } | |
| sb.append("}"); | |
| return sb.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment