Closed jhowardjr closed 6 years ago
Trying to figure out edge cases. 18! is 6402373705728000 which is the last factorial we can calculate before the code will exceed the Number.MIN_SAFE_INTEGER
Number.MAX_SAFE_INTEGER
. Do we limit all parameters to be 18 or less?
i was thinking about this too.. and i'm sure you are meaning MAX_SAFE_INTEGER, maybe notation would be great for this like scientific, but it still would have to be calculated before converted, and just defeating the purpose, so maybe some algorithmic pattern can compute this. Will look into this.
If you limit yourself to integers with factorials less than Number.MAX_SAFE_INTEGER, then you don't need to calculate anything. Just put the factorials into a small lookup table.
@redquark great observation!
Here's the code I have so far: https://github.com/devNoiseConsulting/variadic.js/blob/develop/lib/factorial.js
I'm doing the calculations, but also caching them so that we don't have to calculate every factorial and then using the highest value as the starting point for a new higher factorial.
In any case, I think we need to cap it at 18!. To overcome this we need to bring in a library that supports really big integers or we try to tackle that ourselves. Until either of those happen, I will write my error handling and test to work under that constraint.
Nice memoization!
Request help creating a factorial function that calculates the factorial value of each parameter in the parameter list.