Here I would like to mention the concept of integer clock.
The maximum and minimum values for int in Java are:
int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648
Please check the following results
int a = 2147483645;
for(int i=0; i<10; i++) {
System.out.println("a:" + a++);
}
Output:
a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642
It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.
-2147483648, <-----------------
-2147483647, |
-2147483646, |
. |
. |
. | (the next value will go back in -ve range)
0, |
+1, |
+2, |
+3, |
. |
. |
., |
+2147483645, |
+2147483646, |
+2147483647 ---------------------
If you calculate the factorial of 13 it is 6227020800.
This value goes beyond the int range of java.
So the new value will be
6227020800
- 2147483647 (+ve max value)
-----------------
Value = 4079537153
- 2147483648 (-ve max value)
-----------------
value = 1932053505
- 1 (for zero in between -ve to +ve value)
----------------
Answer = 1932053504
So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.
You can use long datatype instead of integer to achieve your purpose.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…