import java.math.BigInteger;
import java.util.Scanner;
public class LastFact
{
// Returns Factorial of N
static BigInteger factorial(int N)
{
// Initialize result
BigInteger f = new BigInteger("1"); // Or BigInteger.ONE
// Multiply f with 2, 3, ...N
for (int i = 2; i <= N; i++)
f = f.multiply(BigInteger.valueOf(i));
return f;
}
// Driver method
public static void main(String args[]) throws Exception
{
int N = 300;
System.out.println(factorial(N));
}
}
最佳答案
如果要删除 BigInteger
的最后 N
位数字,请将其除以 10^N
,例如删除最后两位数字除以 10^2=100
:
BigInteger i = new BigInteger("123456");
BigInteger j = i.divide(BigInteger.valueOf(100)); // j=1234
要获取要删除的提醒,请使用 reminder()
方法:
BigInteger i = new BigInteger("123456");
BigInteger j = i.reminder(BigInteger.valueOf(100)); // j=56
请注意,由于您正在计算大阶乘,您可能需要使用 Stirling's approximation 。
关于java - 我想取出JAVA中BigInteger中存储的大数的最后一位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633188/