Skip to content

Instantly share code, notes, and snippets.

@rcreasi
Last active July 25, 2019 16:51
Show Gist options
  • Save rcreasi/f44786c4d783b17e184979bec569fff3 to your computer and use it in GitHub Desktop.
Save rcreasi/f44786c4d783b17e184979bec569fff3 to your computer and use it in GitHub Desktop.
function realFactorial(n) {
let result = n;
for (let i = 1; i < n; i++) {
result *= i;
}
return result;
}
function approxFactorial(n) {
return Math.sqrt(2 * Math.PI * n) * Math.pow(n / Math.E, n);
}
let n = 20;
let a = realFactorial(n)
let b = approxFactorial(n);
// I was calculating this backwards. The error rate actually converges *toward* zero. 😂
// Thanks to @etzpcm
let percentOff = (a - b) / a * 100;
console.log(`${a} ~ ${b} (off by ${percentOff.toFixed(2)}%)`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment