求大整數的階乘

package 大整數求三十項和;

// 使用BigInteger類計算1!+3!+5!+。。。。。。的前30項的和
import java.math.*;
public class sum
{

public static void main(String args[]) {
BigInteger sum = new BigInteger("0");
int i = 30;//三十項
Integer j = 1;//當前項
while (0 < i--) {
sum = sum.add(factorial(new BigInteger(j.toString())));
j += 2;
}
System.out.println(sum);
}

public static BigInteger factorial(BigInteger bint) {
if (bint.equals(BigInteger.ZERO) || bint.equals(BigInteger.ONE)) {
return BigInteger.ONE;
}
return bint.multiply(factorial(bint.subtract(BigInteger.ONE)));
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章