First, the Multiplication data overflow
Background: When calculating factorial , when the number is too large, the result is 0, this time leads to data in the multiplication of data overflow 10! or 100!
BYTE 8 bit 8! = -27=-128 (converted to byte allowed data range) 9! Equals 8! 10!=0
Short 16 bit 16! = 215 (converted to short allowable data range) 17! equals 16! 18!=0
int 32 bit 32! =-231 (convert to int allowed data range) 33! Equals 32! 34!=0
Long 64 bit 64! =-263 (convert to long allowed data range) 65! Equals 64! 65!=0
Description equals the negative minimum value when it is equal to the number of its own bits in the data type.
The next factorial is also equal to the minimum negative value (n data lap number, such as Byte is n 28)
The next factorial is equal to 0, that is, n half a circle, such as the byte type is n 128 (27)
Conclusion: when calculating the factorial, the data type and the next one equals the minimum value, plus 2 bits is 0.
The test code is as follows:
byte a = 1;
for (byte n = 1; n < 100;n++) {
A = (byte) (A * n);
SYSTEM.OUT.PRINTLN (n+ "!" + "=" +a);
}
int b = 1;
for (int n = 1; n < 100;n++) {
b = (int) (b * n);
SYSTEM.OUT.PRINTLN (n+ "!" + "=" +b);
}
The test results are as follows:
similarly , short is the same as long type; double because there are decimals, so the result is infinity, the result is as follows
According to the result can be changed to the following mathematical expression, there is no inference to confirm
int sum = 1*2*3*4*.......*32 = -231+n*232 (n is a positive integer, and this is the first expression within 0 to 32, which is the smallest positive integer that can be multiplied by 32 o'clock N).
byte sum = 1*2*3*...*8 = -128+n*28
Second, judging whether the number is a pure number:
1.24 Java Weekend Summary multiplication data Overflow Judging whether the string is a pure number