I have this formula:
f(x)=(x^0+x^0+x^1+x^101+x^2+x^202)^n
which is basically probabilities when rolling n dice with faces showing values:
{0,0,1,101,2,202}
How do I translate that to JavaScript?
I know I can do recursive function to get every single results and add them in an array of results, but anything over 9 dice becomes very long.
EDIT: Note that x is not a variable. It is some fancy mathematique symbol that represent I forgot what about probabilities.
The x in your question is a formal variable, and you're asking for a procedure to compute powers of formal polynomials in that variable. Here's a quick implementation. Warning: I haven't executed it, and in any case it lacks polish. But I hope you get the idea.
function polynomial_unit() {
return [1]; // Represents 1 = 1x^0
}
function polynomial_mult(x, y) {
var z = Array.apply(null, new Array((x.length + y.length - 1))).map(Number.prototype.valueOf,0);
for(var i = 0; i < x.length; i++) {
for(var j = 0; j < y.length; j++) {
z[i+j] += x[i]*y[j];
}
}
return z;
}
function generic_exp(base, exponent, mult, unit) {
var t = unit;
while (exponent > 1) {
if (exponent%2 == 1) t = mult(t, base);
base = mult(base, base);
exponent = Math.floor(exponent/2);
}
return mult(t, base);
}
function Wildhorn(n) {
var f = Array.apply(null, new Array(203)).map(Number.prototype.valueOf,0);
f[0] = 2; f[1] = 1; f[2] = 1; f[101] = 1; f[202] = 1;
return generic_exp(f, n, polynomial_mult, polynomial_unit());
}