Last active
July 25, 2019 16:51
-
-
Save rcreasi/f44786c4d783b17e184979bec569fff3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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