java - 我想取出JAVA中BigInteger中存储的大数的最后一位

标签 java factorial

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/

相关文章:

C#:尽可能高效地将大量文件放入 DVD 的代码

sql - 在 SQL 中,如何生成 5!56 的每个可能的唯一组合?

java - 导出 Excel 并防止用户重新处理操作直至完成的操作方法

java - 当我在 hadoop 中运行 map/reduce 作业时,遇到 XMLJAXBElementProvider 异常

java - 为什么 jaxb 2.2 无法处理元素及其子元素具有相同 ID 的情况?

java - 阶乘函数产生 21 的错误结果!以上

java - 添加长数字时 IndexOutOfBoundsException 错误

java - 二维数组的 CopyOfRange

algorithm - 您将如何编写非递归算法来计算阶乘?

java - 如何编写超阶乘程序?