Java利用BigInteger类求大数阶乘运算

进行大数运算,用到BigInteger类,首先介绍一下这个类

方法描述
public BigInteger (String var)将一个字符串变为BigInteger类型的数据
public BigInteger add(BigInteger val)加法
public BigInteger subtract(BigInteger val)减法
public BigInteger multiply(BigInteger val)乘法
public BigInteger subtract(BigInteger val)除法
import java.math.BigInteger;
import java.util.Scanner;

public class BigFlag{
    public static void main(String[] args) {
        long startTime=System.currentTimeMillis();
        BigInteger num=new BigInteger("1");
        BigInteger flat=new BigInteger("1");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for (int i = 0; i < n; i++) {
            flat=flat.multiply(num);
            num=num.add(new BigInteger("1"));
        }
        System.out.println(n+"!="+flat);
        System.out.println("共"+flat.toString().length()+"位");
        long endTime=System.currentTimeMillis();
        System.out.println("计算"+n+"!一共用了"+(double)(endTime-startTime)/1000+"秒");

    }
}

在这里插入图片描述以100000的阶乘为例,一共用了8秒。

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 使用JavaBigInteger乘的方法如下: 1. 首先,你需要导入 java.math.BigInteger 包。 2. 然后,你可以使用如下代码来一个数的乘: ``` BigInteger result = BigInteger.ONE; for (int i = 1; i <= n; i++) { result = result.multiply(BigInteger.valueOf(i)); } ``` 在这段代码中,n 是要乘的数,result 变量用来保存结果。首先,我们将 result 初始化为 1,然后使用 for 循环将 1 到 n 的乘积累加到 result 中。 注意,BigInteger 类的乘法操作是使用 multiply() 方法实现的,而不是用 * 运算符。 例如,你可以使用如下代码来 5 的乘: ``` BigInteger result = BigInteger.ONE; for (int i = 1; i <= 5; i++) { result = result.multiply(BigInteger.valueOf(i)); } System.out.println(result); // 输出 120 ``` 希望这能帮到你! ### 回答2: 乘是一个正整数连乘积的运算,即n的乘(n!)等于1乘2乘3乘...乘n。 在Java中,使用BigInteger类可以处理大整数运算,可以用它来乘。 首先,导入java.math包中的BigInteger类。然后,我们可以定义一个方法来计算n的乘。方法的参数是一个正整数n,返回类型是BigInteger。方法中,我们定义一个BigInteger类型的变量result,初始化为1。 接下来,使用一个循环从1到n,依次将每个数字与result进行连乘运算,最后将result返回。 具体实现代码如下: ```java import java.math.BigInteger; public class Factorial { public static BigInteger factorial(int n) { BigInteger result = BigInteger.ONE; // 初始化为1 for (int i = 1; i <= n; i++) { result = result.multiply(BigInteger.valueOf(i)); // 连乘运算 } return result; } public static void main(String[] args) { int n = 10; BigInteger result = factorial(n); System.out.println(n + "! = " + result); } } ``` 在以上代码中,我们解了10的乘,并将结果打印输出。运行程序,输出结果为: ``` 10! = 3628800 ``` BigInteger类可以处理大整数运算,因此可以解非常大的乘,远超过普通整型或长整型的范围限制。 ### 回答3: 要使用JavaBigInteger乘,首先需要导入java.math.BigInteger包。BigInteger类是一个不可变的大整数类,可以处理超过long类型所能表示的范围的整数。 然后,我们需要创建一个BigInteger类型的变量来存储乘的结果。由于乘的结果可能非常大,我们可以先将结果初始化为1,即BigInteger result = BigInteger.valueOf(1)。 接下来,使用一个循环来计算乘,从1循环到所需的乘数n。在每次循环中,将当前的BigInteger变量result与当前的循环变量相乘,并将结果赋值给result,即result = result.multiply(BigInteger.valueOf(i))。 最后,循环结束后,result的值即为所需的乘结果。 以下是一个使用JavaBigInteger乘的示例代码: import java.math.BigInteger; public class Factorial { public static void main(String[] args) { int n = 10; // 10的BigInteger result = BigInteger.valueOf(1); for (int i = 1; i <= n; i++) { result = result.multiply(BigInteger.valueOf(i)); } System.out.println("10的乘是:" + result); } } 运行以上程序,输出结果为10的乘值,即3628800。 使用JavaBigInteger类可以轻松计算超大范围内的乘,无需担心整数溢出的问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值