标签:stack turn 资源 flow ati long import public max
import java.math.BigInteger; public class getFactorial { public static void main(String[] args) { // 15511210043330985984000000 System.out.println(getFactorial("25").toString()); // 9223372036854775807 System.out.println(Long.MAX_VALUE); } public static BigInteger getFactorial(BigInteger num) { /** * * 1 大于 1 才继续计算 * 2 while 比 for 少点代码 * 3 int long 范围有限 * 4 递归浪费资源,深度太深jvm不支持,StackOverflowError * 5 使用更专业的计算包 */ BigInteger result = num; while(num.compareTo(BigInteger.ONE) > 0) { num = num.subtract(BigInteger.ONE); result = result.multiply(num); } return result; } public static BigInteger getFactorial(String numString) { return getFactorial(new BigInteger(numString)); } }
标签:stack turn 资源 flow ati long import public max
原文地址:https://www.cnblogs.com/mapleflow/p/10925590.html