Skip to content

Instantly share code, notes, and snippets.

@fesnow
Created September 12, 2013 17:50
Show Gist options
  • Save fesnow/6541336 to your computer and use it in GitHub Desktop.
Save fesnow/6541336 to your computer and use it in GitHub Desktop.
(function($) {
$(function() {
$("#calculate").on("click", execute);
function execute() {
if (typeof($(this).val() * 1) != "number") {
alert("Please input a number.");
} else if ($("#friends").val() * 1 < $("#days").val() * 1) {
$("#answer").empty().append((0).toFixed(20) + " %");
} else {
calculate();
}
}
function calculate() {
var friends = $("#friends").val();
friends = parseInt(friends);
var days = $("#days").val();
days = parseInt(days);
var numerator1 = resolve(friends - 1);
var numerator2 = resolve(friends);
var numerator = numerator1.concat(numerator2);
var denominator1 = resolve(friends + days - 1);
var denominator2 = resolve(friends - days);
var denominator = denominator1.concat(denominator2);
$("#answer").empty().append(reduceCalculate(numerator, denominator));
}
function resolve(x) {
var factArray = [];
x = (x < 1) ? 1 : x;
while (x > 0) {
factArray.push(x--);
}
return factArray;
}
function primeFactorization(x) {
var primeArray = [];
var primeNumber = 2;
while (primeNumber <= x) {
var count = 0;
while (x % primeNumber == 0) {
x = x / primeNumber;
count++;
}
if (count != 0) primeArray[primeNumber] = count;
primeNumber++;
}
return primeArray;
}
function collectPrimeNumber(factArray) {
var gatherArray = [];
$.each(factArray, function() {
var primeArray = primeFactorization(this);
for (var key in primeArray) {
if (gatherArray[key]) {
gatherArray[key] += primeArray[key];
} else {
gatherArray[key] = primeArray[key];
}
}
});
return gatherArray;
}
function reduceCalculate(numeratorArray, denominatorArray) {
var numeratorPrime = collectPrimeNumber(numeratorArray);
var denominatorPrime = collectPrimeNumber(denominatorArray);
for (var key in numeratorPrime) {
if (denominatorPrime[key]) {
var gap = numeratorPrime[key] - denominatorPrime[key];
if (gap > 0) {
numeratorPrime[key] = gap;
delete denominatorPrime[key];
} else if (gap == 0) {
delete numeratorPrime[key];
delete denominatorPrime[key];
}
}
}
for (var key in denominatorPrime) {
if (numeratorPrime[key]) {
var gap = denominatorPrime[key] - numeratorPrime[key];
if (gap > 0) {denominatorPrime[key] = gap;
delete numeratorPrime[key];
}
}
}
var numeratorElem = [];
var denominatorElem = [];
for (var key in numeratorPrime) {
numeratorElem.push(Math.pow(key, numeratorPrime[key]));
}
for (var key in denominatorPrime) {
denominatorElem.push(Math.pow(key, denominatorPrime[key]));
}
var answerArray = [];
$.each(denominatorElem, function(i) {
var numerator = numeratorElem[i];
if (numerator) {
answerArray.push((numerator / this).toFixed(20));
} else {
answerArray.push((1 / this).toFixed(20));
}
});
var numElmLength = numeratorElem.length;
var denElmLength = denominatorElem.length;
if (numElmLength > denElmLength) {
for (var i = 0; i <= numElmLength - denElmLength - 1; i++) {
answerArray.push(numeratorElem[i + denElmLength]);
}
}
var answer = 1;
$.each(answerArray, function() {
answer *= this;
});
return (answer * 100).toFixed(20) + " %";
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment