Anticipate factorial overflow

I am wondering how I could expect if the next iteration will generate integer overflow when calculating factorial F or not?

Let's say that at each iteration I have int I, and the maximum value is MAX_INT.

That sounds like homework, I know. Not this. I just ask myself "stupid" questions.

Adding

At least about this, given several BITS (the width that an integer can take in bits), I could round the number i to the next power of two and determine whether the left shift will exceed BITS. But how will it look algorithmically?

+5
source share
4 answers

Alternative hint:

a * b ≤ MAX_INT 

equivalently

a ≤ MAX_INT / b

if b> 0.

+9
source

, , , . , , , . , , .

+4

m = (n-1)!, n, , ,

   m <= MAX_INT / n
+2

, , ,

ln (n!) = n * ln (n) - n + ln (2 * pi * n)/2 + O (1/n)

.

In fact, you do not need to try to breed, etc. Of course, this does not answer what you asked, but considering that you are just interested, I hope this helps.

+1
source

All Articles