Tag: value store style str problem new out bug ToString
Just learn the Java loop, to find the factorial problem, found that 10! You can use the int type to store the value,
What about 100? With long? Found with a long type, the result of running the output is 0, this is not true,
What is the reason? Debug with debug found, 100! Data is too large, long data storage space 8 bytes is not enough to overflow.
So what is the way to achieve 100?
The following code:
1PublicClassDemo01 {2PublicStaticvoidMain (string[] args) {3 System.out.println (factorial (100));4}56PublicStatic String factorial (Int count) { 7 BigInteger result = new BigInteger (string.valueof (1< Span style= "color: #000000")); 8 for (int i = 1; I <= count ; I++) { 9 BigInteger I_value = new BigInteger (string.valueof (i)); Ten result = result.multiply (i_value); }12 return result.tostring (); 13 }14}
Resolves a memory overflow problem
Java Implementation 100! The factorial