代码
public static void main(String[] args) {
//ok
//System.out.println("result:"+factorial(12));
//int最大 2^32 4,294,967,296 到这就溢出了,算出来的数不对
//System.out.println("result:"+factorial(13));
//改成long
//ok
//System.out.println("result:"+factorialLong(20));
//long最大 2^64 18,446,744,073,709,600,000 到这就溢出了,算出来的数不对
System.out.println("result:"+factorialLong(21));
}
public static int factorial(int i){
System.out.println(i);
if(i<=1) {
return 1;
};
return i*factorial(i-1);
}
public static long factorialLong(long i){
System.out.println(i);
if(i<=1) {
return 1;
};
return i*factorialLong(i-1);
}
所以怎么办呢
BigInteger