improve bigFactorial algorithm
See original GitHub issueYou can use different algorithms of factorial to speed this 2-8 times. Universal one (twice as fast): [i am using bigint’s in implementation, but logic is the same]
function factorial(n) {
if(n === 0n || n === 1n) {
return 1n;
}
if(n % 2n === 1n) {
return n * factorial(n - 1n);
}
let p = n;
let prod = n;
while(p > 2n) {
p -= 2n;
n += p;
prod *= n;
}
return prod;
}
If you are interested in the less universal (faster than this after n=1400), I could provide it in comment
Issue Analytics
- State:
- Created 4 years ago
- Comments:37 (37 by maintainers)
Top Results From Across the Web
Factorial algorithm more efficient than naive multiplication
Computing each power can be done efficiently using repeated squaring, and then the factors are multiplied together. This was described by Peter ...
Read more >Algorithm to Compute Factorial of 100 OR More.
Algorithm to Compute Factorial of 100 OR More. ... In this method we just want to increase the size of array to get...
Read more >Highly! specific method for very big factorial calculation in php
i have been working on a statistical web app that calculates various statistical parameters sometimes its needed to calculate very large ...
Read more >How to calculate large factorial using BigInteger in Java ...
Here are some key points about java. 2. The BigInteger class is immutable which means that the object on which the multiply function...
Read more >Find the Factorial of a large number - GeeksforGeeks
Put all digits of carry in res[] and increase res_size by the number of digits in carry. Below is the implementation of the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
👍 good luck with your exams
@josdejong the problem is that
BigInt(5)
is slow, and5n
will throw an error if a browser doesn’t support BigInt 😕 well… I’ll see what can be done to squeeze as much performance as I can