用高精度计算出 � = 1 ! + 2 ! + 3 ! + ⋯ + � ! S=1!+2!+3!+⋯+n!( � ≤ 50 n≤50)java
时间: 2023-08-05 18:10:12 浏览: 138
以下是使用Java计算高精度的代码示例:
```java
import java.math.BigInteger;
public class FactorialSum {
public static void main(String[] args) {
int n = 50;
BigInteger sum = BigInteger.ZERO;
for (int i = 1; i <= n; i++) {
BigInteger factorial = calculateFactorial(i);
sum = sum.add(factorial);
}
System.out.println("Sum: " + sum);
}
private static BigInteger calculateFactorial(int num) {
BigInteger factorial = BigInteger.ONE;
for (int i = 1; i <= num; i++) {
factorial = factorial.multiply(BigInteger.valueOf(i));
}
return factorial;
}
}
```
这段代码会计算从1到50的阶乘,并将它们相加得到总和。使用BigInteger类可以处理大整数计算,以确保高精度结果。运行后,将打印出结果。
阅读全文