public class MaxFactorialSum {
public static void main(String[] args) {
int sum = 0; // 存储阶乘累加和
int factorial = 1; // 动态计算阶乘(初始为1!)
int n = 1; // 当前阶乘序号
// 使用递推算法动态计算阶乘和
while (true) {
sum += factorial; // 累加当前阶乘
if (sum > 9999) {
System.out.println("最大整数n为: " + (n - 1));
break;
}
n++; // 递增阶乘序号
factorial *= n; // 计算下一个阶乘值(n! = (n-1)! * n)
}
}
}