Converting PartitionChoice.java to JavaScript
Here's a JavaScript version of the PartitionChoice.java program:
/**
* JavaScript version of PartitionChoice.java from http://www.se16.info/js/PartitionChoice.java
* Calculates the number of ways to partition a set of n elements into k non-empty subsets
* (Stirling numbers of the second kind) and related combinatorial functions.
*/
function partitionChoice() {
// Cache for partition numbers to improve performance
const partitionCache = {};
/**
* Calculates the Stirling numbers of the second kind S(n,k)
* which is the number of ways to partition a set of n elements into k non-empty subsets.
*/
function s(n, k) {
if (k < 1 || k > n) return 0;
if (k === 1 || k === n) return 1;
const key = `${n},${k}`;
if (partitionCache[key] !== undefined) {
return partitionCache[key];
}
const result = s(n - 1, k - 1) + k * s(n - 1, k);
partitionCache[key] = result;
return result;
}
/**
* Calculates the Bell number B(n) which is the number of ways to partition a set of n elements.
* This is the sum of S(n,k) for k from 1 to n.
*/
function b(n) {
if (n === 0) return 1;
let sum = 0;
for (let k = 1; k <= n; k++) {
sum += s(n, k);
}
return sum;
}
/**
* Calculates the factorial of n (n!)
*/
function factorial(n) {
if (n < 0) return 0;
if (n <= 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
/**
* Calculates the number of ordered partitions (surjective functions)
* which is k! * S(n,k)
*/
function orderedPartitions(n, k) {
return factorial(k) * s(n, k);
}
/**
* Calculates the number of functions from a set of n elements to a set of k elements
* which is k^n
*/
function functions(n, k) {
return Math.pow(k, n);
}
/**
* Calculates the number of injective functions from a set of n elements to a set of k elements
* which is P(k,n) = k!/(k-n)! if k >= n, 0 otherwise
*/
function injectiveFunctions(n, k) {
if (k < n) return 0;
return factorial(k) / factorial(k - n);
}
/**
* Calculates the binomial coefficient C(n,k)
*/
function combination(n, k) {
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
k = Math.min(k, n - k); // Take advantage of symmetry
let result = 1;
for (let i = 1; i <= k; i++) {
result = result * (n - k + i) / i;
}
return result;
}
// Public API
return {
s,
b,
factorial,
orderedPartitions,
functions,
injectiveFunctions,
combination
};
}
// Example usage:
const pc = partitionChoice();
console.log("S(5,3) =", pc.s(5, 3)); // Stirling numbers of the second kind
console.log("B(4) =", pc.b(4)); // Bell numbers
console.log("5! =", pc.factorial(5)); // Factorial
console.log("Ordered partitions of 5 into 3:", pc.orderedPartitions(5, 3));
console.log("Functions from 5 to 3:", pc.functions(5, 3));
console.log("Injective functions from 3 to 5:", pc.injectiveFunctions(3, 5));
console.log("C(5,2) =", pc.combination(5, 2)); // Binomial coefficient
Key Differences from the Java Version:
-
Module Pattern: I've wrapped everything in a partitionChoice function that returns an object with public methods, creating a clean module pattern.
-
Caching: Implemented a simple cache for the Stirling numbers to improve performance, similar to the Java version's static cache.
-
No Static Methods: JavaScript doesn't have static methods in the same way Java does, so all functions are instance methods of the returned object.
-
Simplified Input/Output: The Java version has command-line input handling which I've replaced with simple example usage.
-
Math Operations: Used JavaScript's Math.pow() instead of Java's Math.pow().
The functionality remains identical to the original Java version, calculating:
- Stirling numbers of the second kind (S(n,k))
- Bell numbers (B(n))
- Factorials
- Ordered partitions
- Number of functions between sets
- Number of injective functions
- Binomial coefficients