logoalt Hacker News

qsorttoday at 6:46 PM1 replyview on HN

You don't need space for 40!/20!, for example:

  let ans = 1
  for (let i=1; i<21; ++i) {
    ans *= (41 - i)
    ans /= i
  }
The same idea can be trivially tweaked to compute any binomial coefficient without ever storing an integer greater than the final result.

Replies

ogogmadtoday at 7:12 PM

Good point. But what if `i` does not divide `ans` evenly? I suppose you could use floats and then round.

show 1 reply