I know there must be a trick I am missing, how do I calculate past 33! using in browser JS? I have the following already, this gets me to 33 but blows up when I go to 34. I Got 34 to work in Node like this...
node --max-old-space-size=4096 --experimental-modules test.mjs
But it blows up again at 35.
Can I calculate past 33! using JS in the browser?
Such large numbers aren't supported natively, so one option is to use a library such as BigInteger:
const factorial = n => bigInt(n).multiply(
n === 1
? 1
: factorial(n - 1)
);
console.log(factorial(45));
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.34/BigInteger.min.js"></script>
(when using it, make sure to pass large numbers as strings or other bigInt
s to avoid precision loss)