用java计算:输入任意正整数n,代入此公式:(10/2的阶乘)+(20/4的阶乘)+(40/6的阶乘)+……+[(10*2的n次方)/(2*n的阶乘)],输出答案
时间: 2023-10-25 18:06:35 浏览: 161
好的,我来为您写一段Java代码来计算这个公式:
```java
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
BigInteger sum = BigInteger.valueOf(0);
BigInteger numerator = BigInteger.valueOf(10);
BigInteger denominator = BigInteger.valueOf(2);
for (int i = 1; i <= n; i++) {
BigInteger factorial = BigInteger.valueOf(1);
for (int j = 1; j <= i * 2; j++) {
factorial = factorial.multiply(BigInteger.valueOf(j));
}
sum = sum.add(numerator.divide(denominator.multiply(factorial)));
numerator = numerator.multiply(BigInteger.valueOf(2));
denominator = denominator.multiply(BigInteger.valueOf(i * 2));
}
System.out.println(sum);
}
}
```
代码中使用了 `BigInteger` 类型来处理超过 `long` 类型范围的大数运算。运行程序后,输入任意正整数 `n`,即可得到公式的计算结果。
阅读全文