Skip to content

Instantly share code, notes, and snippets.

@skrosoft
Created October 2, 2015 01:49
Show Gist options
  • Save skrosoft/71d62a087ab5ede693df to your computer and use it in GitHub Desktop.
Save skrosoft/71d62a087ab5ede693df to your computer and use it in GitHub Desktop.
function processData(input) {
var n = parseInt(input);
var number = numberToArray(n);
while(n > 1){
number = multiplyArray(number, numberToArray(n-1));
n--;
}
process.stdout.write(number.reverse().join(''));
}
var numberToArray = function(num){
num += "";
var digits = [];
for(var i=0; i < num.length; i++){
digits.push(parseInt(num[i]));
}
return digits.reverse();
};
var multiplyArray = function(arr1, arr2){
//process.stdout.write(arr1.join('') + "\n\n");
//process.stdout.write(arr2.join('') + "\n\n");
//process.stdout.write("------ \n\n");
var result = [];
for (i = 0; i < arr1.length; ++i) {
for (retenue = 0, j = 0; j < arr2.length || retenue > 0; ++j) {
result[i + j] = (retenue += (result[i + j] || 0) + arr1[i] * (arr2[j] || 0)) % 10;
retenue = Math.floor(retenue / 10);
}
}
//process.stdout.write("RESULT: " + result.join('') + "\n\n");
//process.stdout.write("------ \n\n");
return result;
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment