Home </br>Permutation Generators </br>Combination Generators </br>Set/Subset Generators </br>Cartesian Product Generators </br>Math Functions </br>Ranking Algorithms </br>Number System Algorithms
The Calculator
class in JNumberTools provides efficient, thread-safe mathematical functions for combinatorics and number theory computations. It uses memoization for performance, supporting operations like factorials, binomial coefficients, multinomial coefficients, and more, with BigInteger
compatibility for large-scale calculations.
Key Features:
nCr
, nPr
, factorial, and subfactorial values.BigInteger
.Currently Available Functions
Computes the factorial of a non-negative integer n
(n!), using a memoized cache for efficiency.
Calculator calc = new Calculator();
// Computes 5! = 120
BigInteger result = calc.factorial(5);
System.out.println(result); // Output: 120
Calculates the subfactorial (!n), the number of derangements of n items, using memoization.
Calculator calc = new Calculator();
BigInteger result = calc.subFactorial(4);
System.out.println(result); // Output: 9
Finds the smallest n such that n! exceeds a given value.
Calculator calc = new Calculator();
int result = calc.factorialUpperBound(BigInteger.valueOf(1000));
System.out.println(result); // Output: 7
Calculates nCr, the number of ways to choose r items from n without repetition, using memoization.
Calculator calc = new Calculator();
BigInteger result = calc.nCr(5, 2);
System.out.println(result); // Output: 10
Computes the binomial coefficient with repetition allowed, equivalent to C(n + r - 1, r).
Calculator calc = new Calculator();
BigInteger result = calc.nCrRepetitive(4, 3);
System.out.println(result); // Output: 20
Finds the smallest n such that nCr(n, r) exceeds a given value.
Calculator calc = new Calculator();
int result = calc.nCrUpperBound(3, BigInteger.valueOf(100));
System.out.println(result); // Output: 9
Calculates nPr, the number of ways to arrange r items from n, using memoization.
Calculator calc = new Calculator();
BigInteger result = calc.nPr(5, 3);
System.out.println(result); // Output: 60
Computes the multinomial coefficient for given counts, representing distinct permutations of a multiset.
Calculator calc = new Calculator();
BigInteger result = calc.multinomial(2, 1, 3);
System.out.println(result); // Output: 60
Calculates the total number of m-th permutations for a multiset, given a start index and step m.
Calculator calc = new Calculator();
BigInteger result = calc.totalMthMultinomial(0, 2, 2, 1, 3);
System.out.println(result); // Output: 30
Computes the number of ways to select exactly s items from a multiset for all s in the range [0, ⌊total⌋/2], where total is the sum of the frequencies. Returns an array of counts for selecting 0 to ⌊total/2⌋ items, using dynamic programming.
int[] result = Calculator.multisetCombinationsCountAll(2, 1);
System.out.println(Arrays.toString(result)); // Output: [1, 2, 1]
Calculates the number of ways to select exactly k items from a multiset.
BigInteger result = Calculator.multisetCombinationsCount(2, 2, 1);
System.out.println(result); // Output: 2
Counts the number of ways to select exactly k items from a multiset defined by frequencies, considering only the item types from the specified index onward.
int k = 5;
int index = 2;
int[] frequencies = {10, 12, 5, 8};
BigInteger result = Calculator.multisetCombinationsCountStartingFromIndex(k, index, frequencies);
Computes the total number of subsets of sizes within a given range from n elements.
Calculator calc = new Calculator();
BigInteger result = calc.totalSubsetsInRange(1, 2, 4);
System.out.println(result); // Output: 10
Calculates base raised to exponent, supporting both long and BigInteger inputs.
Calculator calc = new Calculator();
BigInteger result = calc.power(2, 10);
System.out.println(result); // Output: 1024
Computes the GCD of multiple BigInteger numbers using the binary GCD algorithm.
Calculator calc = new Calculator();
BigInteger result = calc.gcd(BigInteger.valueOf(48), BigInteger.valueOf(18), BigInteger.valueOf(27));
System.out.println(result); // Output: 3
Computes the LCM of multiple BigInteger numbers using a hybrid iterative/recursive approach.
Calculator calc = new Calculator();
BigInteger result = calc.lcm(BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8));
System.out.println(result); // Output: 24
Calculates the number of permutations of n items with exactly k fixed points.
Calculator calc = new Calculator();
BigInteger result = calc.rencontresNumber(4, 1);
System.out.println(result); // Output: 8
Home </br>Permutation Generators </br>Combination Generators </br>Set/Subset Generators </br>Cartesian Product Generators </br>Math Functions </br>Ranking Algorithms </br>Number System Algorithms