Skip to content

Instantly share code, notes, and snippets.

@gocreating
Created June 17, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gocreating/1a3fdb9df5974c09adfc to your computer and use it in GitHub Desktop.
Save gocreating/1a3fdb9df5974c09adfc to your computer and use it in GitHub Desktop.
var f = function(n) {
if (n <= 1) {
return 1;
} else {
return n * f(n - 1);
}
}
var getP0 = function(y) {
var sum = 0;
for (var i = 0; i <= y; i++) {
sum += (Math.pow(7, i) / f(i));
}
for (var i = y + 1; i <= 20; i++) {
sum += (Math.pow(7, y) * Math.pow(2, i - y) / f(i));
}
return 1 / sum;
};
var getPi = function(y, i, P0) {
if (0 <= i && i <= y) {
return (Math.pow(7, i) / f(i)) * P0;
} else if (y < i && i <= 20) {
return (Math.pow(7, y) * Math.pow(2, i - y) / f(i)) * P0;
}
};
var getBOplusBH = function(y) {
var P0 = getP0(y);
var BO = 0;
for (var i = y; i <= 20; i++) {
BO += getPi(y, i, P0);
};
var BH;
BH = (Math.pow(7, y) * Math.pow(2, 20 - y) / f(20)) * P0;
return BO + BH;
};
var min = 999999999;
var ymin;
for (var y = 1; y < 20; y++) {
var BOplusBH = getBOplusBH(y);
console.log('y = ' + y + '\t', 'BO + BH = ' + BOplusBH);
if (BOplusBH < min) {
min = BOplusBH;
ymin = y;
}
}
console.log('min y = ' + ymin);
console.log('min BO + BH = ' + min);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment