You complain that
N!
stop overflowing the 32 bits output variable when N>=34
meaning that, after 34!
, the result keeps remaining to 0.
Well, the answer is that it doesn't stop overflowing. What happens is that the value shown, that is the remainder of the division N! / 2^32
starting from N=34 becomes 0 and will never change.
In order to understand how it can happen, let's start with an example using a decimal number. We'll display the result of N! using a display with only two digits:
Factorial |
Actualresult |
Displayed result |
Notes |
1! |
1 |
01 |
|
2! |
2 |
02 |
|
3! |
6 |
06 |
|
4! |
24 |
24 |
|
5! |
120 |
20 |
Overflow! |
6! |
720 |
20 |
Overflow! |
7! |
5040 |
40 |
Overflow! |
8! |
40320 |
20 |
Overflow! |
9! |
362880 |
80 |
Overflow! |
10! |
3628800 |
00 |
Overflow and the shown value is 00! |
11! |
39916800 |
00 |
Overflow and the shown value is still 00! |
12! |
479001600 |
00 |
Overflow and the shown value is still 00! |
.. |
.. |
00 |
The shown value will be 00 forever |
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…