Skip to content

Instantly share code, notes, and snippets.

@AlexisTM
Created May 18, 2020 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexisTM/ad72976d89da8360cd9ddd4d120701b7 to your computer and use it in GitHub Desktop.
Save AlexisTM/ad72976d89da8360cd9ddd4d120701b7 to your computer and use it in GitHub Desktop.
Very fast algorithm for the a first approximation of a very large factorial (10 decimals)
function factorial(number) {
let result = 0;
for(let i = number; i > 0; --i) {
result += Math.log10(i);
}
return result;
}
function scientific_factorial(number) {
let fact = factorial(number);
let value = Math.pow(10, fact%1);
let exponent = Math.round(fact);
return [value, exponent];
}
let result = scientific_factorial(100000);
console.log("value:", result[0], "exponent:", result[1]);
console.log(result[0].toString() + "e" + result[1].toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment