Java: 304 260 257
I saved some bytes by compacting the memoization function a bit and removing f(n)
entirely, replacing it with direct array access.
BigInteger[]c;BigInteger a(int n,int k){m(n);return g(n).divide(g(n-k)).divide(g(k));}BigInteger g(int n){return n<3?BigInteger.ONE:g(n-1).multiply(c[n-1]);}void m(int n){c=new BigInteger[n];for(int i=0;i<n;++i)c[i]=(i<2)?BigInteger.ONE:c[i-2].add(c[i-1]);}
Unfortunately, BigInteger
is required due to overflows and I had to add memoization. Even on a generation 6 i7, it was taking way too long to run with large inputs.
Ungolfed, with boilerplate class
and main
code:
import java.math.BigInteger;
public class ComputeTheFibonomialCoefficient {
public static void main(final String[] args) {
// @formatter:off
String[][] testData = new String[][] {
{ "0", "0", "1" },
{ "1", "1", "1" },
{ "2", "0", "1" },
{ "3", "2", "2" },
{ "8", "3", "1092" },
{ "11", "5", "1514513" },
{ "22", "7", "7158243695757340957617" },
{ "25", "3", "49845401197200" },
{ "50", "2", "97905340104793732225" },
{ "100", "1", "354224848179261915075" }
};
// @formatter:on
for (String[] data : testData) {
System.out.println("a(" + data[0] + ", " + data[1] + ")");
System.out.println(" Expected -> " + data[2]);
System.out.print(" Actual -> ");
System.out.println(new ComputeTheFibonomialCoefficient().a(
Integer.parseInt(data[0]), Integer.parseInt(data[1])));
System.out.println();
}
}
// Begin golf
BigInteger[] c;
BigInteger a(int n, int k) {
m(n);
return g(n).divide(g(n - k)).divide(g(k));
}
BigInteger g(int n) {
return n < 3 ? BigInteger.ONE : g(n - 1).multiply(c[n - 1]);
}
void m(int n) {
c = new BigInteger[n];
for (int i = 0; i < n; ++i)
c[i] = (i < 2) ? BigInteger.ONE : c[i - 2].add(c[i - 1]);
}
// End golf
}
Program output:
a(0, 0)
Expected -> 1
Actual -> 1
a(1, 1)
Expected -> 1
Actual -> 1
a(2, 0)
Expected -> 1
Actual -> 1
a(3, 2)
Expected -> 2
Actual -> 2
a(8, 3)
Expected -> 1092
Actual -> 1092
a(11, 5)
Expected -> 1514513
Actual -> 1514513
a(22, 7)
Expected -> 7158243695757340957617
Actual -> 7158243695757340957617
a(25, 3)
Expected -> 49845401197200
Actual -> 49845401197200
a(50, 2)
Expected -> 97905340104793732225
Actual -> 97905340104793732225
a(100, 1)
Expected -> 354224848179261915075
Actual -> 354224848179261915075
1335
values in the Fibonomial Coefficient sequence. \$\endgroup\$a(50, 2)
test case missing a leading9
? \$\endgroup\$