logoalt Hacker News

ogogmadtoday at 7:12 PM1 replyview on HN

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


Replies

qsorttoday at 7:22 PM

It always divides it evenly, that's why it works.

After the i-th iteration of the for loop, ans will contain n!/((n-i)!i!) which is exactly \binom{n}{i}, an integer.

Technically "ans" can grow above the final result in my example, but even that could be fixed if one really wants (e.g. i must divide either ans or n-i, you play a bit with divmod to figure out which division you do first.)