question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

improve bigFactorial algorithm

See original GitHub issue

You 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:closed
  • Created 4 years ago
  • Comments:37 (37 by maintainers)

github_iconTop GitHub Comments

1reaction
josdejongcommented, Feb 1, 2020

👍 good luck with your exams

1reaction
bartekleoncommented, Feb 1, 2020

@josdejong the problem is that BigInt(5) is slow, and 5n 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found