Java 当您需要存储(非常)大的数字时,该怎么办?

Java 当您需要存储(非常)大的数字时,该怎么办?,java,numbers,long-integer,Java,Numbers,Long Integer,我试图做一个问题,但它涉及到添加一个非常大的数字。(100!) 使用javaint和long太小了 感谢您的建议java.lang.BigInteger或java.lang.BigDecimal类 看起来它可能就是你要找的东西。使用。下面是一个来自《Java示例》一书的简单例子,涉及到计算阶乘和缓存 import java.math.BigInteger; import java.util.ArrayList; /* * Copyright (c) 2000 David Flanagan.

我试图做一个问题,但它涉及到添加一个非常大的数字。(100!)

使用javaint和long太小了


感谢您的建议

java.lang.BigInteger或java.lang.BigDecimal

类 看起来它可能就是你要找的东西。

使用。下面是一个来自《Java示例》一书的简单例子,涉及到计算阶乘和缓存

import java.math.BigInteger;
import java.util.ArrayList;

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or 
 * implied. You may study, use, and modify it for any non-commercial 
 * purpose. You may distribute it non-commercially as long as you 
 * retain this notice. For a commercial use license, or to purchase 
 * the book (recommended), visit 
 * http://www.davidflanagan.com/javaexamples2.
 */

/**
 * This program computes and displays the factorial of a number 
 * specified on the command line. It handles possible user input 
 * errors with try/catch.
 */
public class FactComputer {
  public static void main(String[] args) {
    // Try to compute a factorial.
    // If something goes wrong, handle it in the catch clause below.
    try {
      int x = Integer.parseInt(args[0]);
      System.out.println(x + "! = " + Factorial4.factorial(x));
    }
    // The user forgot to specify an argument.
    // Thrown if args[0] is undefined.
    catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("You must specify an argument");
      System.out.println("Usage: java FactComputer <number>");
    }
    // The argument is not a number. Thrown by Integer.parseInt().
    catch (NumberFormatException e) {
      System.out.println("The argument you specify must be an integer");
    }
    // The argument is < 0. Thrown by Factorial4.factorial()
    catch (IllegalArgumentException e) {
      // Display the message sent by the factorial() method:
      System.out.println("Bad argument: " + e.getMessage());
    }
  }
}

/**
 * This version of the program uses arbitrary precision integers, so it 
 * does not have an upper-bound on the values it can compute. It uses an 
 * ArrayList object to cache computed values instead of a fixed-size 
 * array. An ArrayList is like an array, but can grow to any size. The 
 * factorial() method is declared "synchronized" so that it can be safely 
 * used in multi-threaded programs. Look up java.math.BigInteger and
 * java.util.ArrayList while studying this class.
 * Prior to Java 1.2, use Vector instead of ArrayList
 */

class Factorial4 {
  protected static ArrayList table = new ArrayList(); // create cache
  static { // Initialize the first element of the cache with !0 = 1.
    table.add(BigInteger.valueOf(1));
  }

  /** The factorial() method, using BigIntegers cached in a ArrayList */
  public static synchronized BigInteger factorial(int x) {
    if (x < 0)
      throw new IllegalArgumentException("x must be non-negative.");
    for (int size = table.size(); size <= x; size++) {
      BigInteger lastfact = (BigInteger) table.get(size - 1);
      BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
      table.add(nextfact);
    }
    return (BigInteger) table.get(x);
  }

  /**
   * A simple main() method that we can use as a standalone test 
   * program for our factorial() method.
   */
  public static void main(String[] args) {
    for (int i = 0; i <= 50; i++)
      System.out.println(i + "! = " + factorial(i));
  }
}
import java.math.biginger;
导入java.util.ArrayList;
/*
*版权所有(c)2000 David Flanagan。版权所有。
*这段代码来自《Java示例简而言之》第二版。
*它是按原样提供的,没有任何明示或暗示的保证
*暗示。您可以为任何非商业目的研究、使用和修改它
*目的。只要您愿意,您可以非商业性地分发它
*保留此通知。申请商业使用许可证,或购买
*本书(推荐),请访问
* http://www.davidflanagan.com/javaexamples2.
*/
/**
*此程序计算并显示数字的阶乘
*在命令行上指定。它处理可能的用户输入
*try/catch错误。
*/
公用计算机{
公共静态void main(字符串[]args){
//试着计算一个阶乘。
//如果出现问题,请在下面的catch子句中处理。
试一试{
intx=Integer.parseInt(args[0]);
System.out.println(x+“!=”+阶乘4.阶乘(x));
}
//用户忘记指定参数。
//如果参数[0]未定义,则引发。
捕获(阵列索引边界外异常e){
System.out.println(“必须指定参数”);
System.out.println(“用法:JavaFactComputer”);
}
//参数不是数字。由Integer.parseInt()引发。
捕获(数字格式){
System.out.println(“指定的参数必须是整数”);
}
//参数<0。由Factorial4.factorial()引发
捕获(IllegalArgumentException e){
//显示factorial()方法发送的消息:
System.out.println(“错误参数:+e.getMessage());
}
}
}
/**
*这个版本的程序使用任意精度的整数,所以
*没有它可以计算的值的上限。它使用一个
*ArrayList对象以缓存计算值而不是固定大小
*数组。ArrayList类似于数组,但可以扩展到任何大小。这个
*factorial()方法声明为“synchronized”,因此可以安全地
*用于多线程程序中。查找java.math.biginger并
*学习这个类时,java.util.ArrayList。
*在Java1.2之前,请使用Vector而不是ArrayList
*/
类阶乘4{
受保护的静态ArrayList表=新建ArrayList();//创建缓存
静态{//使用!0=1初始化缓存的第一个元素。
表.add(biginger.valueOf(1));
}
/**factorial()方法,使用ArrayList中缓存的大整数*/
公共静态同步BigInteger阶乘(int x){
if(x<0)
抛出新的IllegalArgumentException(“x必须是非负的”);
对于(int size=table.size();size
import java.math.biginger;
导入java.util.*;
公共班机{
受保护的静态ArrayList表=新ArrayList();
静止的{
表.add(biginger.valueOf(1));
}
公共静态同步BigInteger阶乘(int x){
如果(x<0)抛出新的IllegalArgumentException(“x必须是非负的”);

对于(int size=table.size();size-Huh?没有java.lang.Bignum.java.Math.BigInteger和BigDecimal,当然。但是没有Bignum.Ahhh…我看到BigInteger和BigDecimal在1.1测试版中被删除,从未随java 1.1发布。抱歉,我的内存再次老化
import java.math.BigInteger;
import java.util.*;

public class Main {
    protected static ArrayList<BigInteger> table = new ArrayList<BigInteger>();

    static {
        table.add(BigInteger.valueOf(1));
    }

    public static synchronized BigInteger factorial(int x) {
        if (x < 0) throw new IllegalArgumentException("x must be non-negative.");
        for (int size = table.size(); size <= x; size++) {
            BigInteger lastfact = table.get(size - 1);
            BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
            table.add(nextfact);
        }
        return table.get(x);
    }

    public static void main(String[] args) {
        for (int i = 0; i <= 50; i++)
            System.out.println(i + "! = " + factorial(i));
    }
}