`
starbhhc
  • 浏览: 649552 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

BigInteger使用例子

阅读更多
import java.math.BigInteger;   
import java.util.ArrayList;   
  
/**  
* 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));   
  }   
}   
分享到:
评论

相关推荐

    Java中BigInteger方法总结

    下面是一些简单的示例,展示了如何使用 `BigInteger` 类的一些方法: ```java public class BigIntegerExample { public static void main(String[] args) { // 创建BigInteger对象 BigInteger a = new ...

    RSA.rar_BigInteger_RSA BigInteger_RSA java biginteger_RSA 类 java

    在Java中,我们可以使用`java.math.BigInteger`类来处理大整数,这在实现RSA算法时非常关键,因为加密过程中涉及的数字通常超过了普通整型变量的范围。以下是关于`BigInteger`类以及如何在Java中实现RSA加解密的详细...

    BigInteger_src.zip

    而BigIntegerDoc.html则可能是关于这个自定义BigInteger类的文档,提供了类的使用说明、方法详细描述以及可能的示例代码,帮助开发者更好地理解和使用这个类。 在实际应用中,生成RSA公钥和私钥的过程涉及大数的...

    BigInteger BigDecimal 使用

    `com`这个文件夹名可能表示该压缩包中包含的是一个Java项目的源代码目录,其中可能包含了使用`BigInteger`和`BigDecimal`的例子或库。在实际学习和使用时,可以查看这些源代码来理解这两个类的具体用法和应用场景。...

    BigInteger_demo.zip

    标题中的"BigInteger_demo.zip"表明这是一个关于Java的大整数处理示例程序的压缩包,其中可能包含一个名为"BigInteger.exe"的应用程序。这个程序很可能是用Java编程语言编写的,利用了Java的BigInteger类来处理大...

    Java中BigInteger.docx

    在给定的代码示例中,我们看到了如何使用 `BigInteger` 进行大数运算。首先,让我们理解一下提供的代码片段: ```java Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); ...

    java练习_大数运算_BigInteger.pdf

    下面是一个使用BigInteger类进行大数运算的示例代码: ```java import java.math.BigInteger; public class BigIntegerExample { public static void main(String[] args) { BigInteger k = BigInteger.valueOf...

    Java中两个大数之间的相关运算及BigInteger代码示例

    Java中两个大数之间的相关运算及BigInteger代码示例 Java 中的两个大数之间的相关运算是指在 Java programming 语言中对大整数的加减乘除运算。这些运算通常用于financial、scientific 和 cryptographic 应用程序中...

    BigInteger:为Java实现BigInteger

    本文将深入探讨`BigInteger`类的使用方法、主要功能和一些关键知识点。 1. **类介绍** `BigInteger`是一个不可变的任意精度的整数类。这意味着一旦创建了一个`BigInteger`实例,就不能修改它的值。它的设计允许...

    node-biginteger:java.math.BigInteger的node.js版本

    例子 $ npm install node-biginteger var BigInteger = require('node-biginteger'); var n = BigInteger.fromString('abc', 16); n.toString(16); 类方法:BigInteger.fromLong(val) 瓦尔朗 返回:...

    biginteger:大整数的另一个 C 实现,及其应用示例

    在这个示例中,可能会展示如何使用这个BigInteger库来实现RSA算法的关键步骤,如大数的幂运算和模逆运算。 “$ make”和“$ make run”是Unix或Linux下的命令行操作,用于编译和运行项目。这暗示了这个大整数库是一...

    C# 4.0 大数的运算--BigInteger的应用详解

    在本文中,我们主要探讨了两个使用 `BigInteger` 进行大数运算的例子: 1. 判断大数是否为质数: 判断一个大数是否为质数,通常需要检查其是否能被小于等于其平方根的任何正整数整除。在C#中,由于 `BigInteger` ...

    biginteger.js:一个任意大小的 javascript 整数库。 实现所有基本算术函数,包括加法、减法、乘法、除法、取模和求幂

    例子 是一个小而有用的网络应用程序,它展示了 biginteger.js 的素性检查的强大功能。 参考 长除法和取模的算法改编自 Per Brinch Hansen 的 。 Jerry Shurman 从提取了一种有效的模幂算法。 执照 麻省理工学院执照...

    计算机代数系统Algebrite.zip

    Algebrite 是用 Javascript (Coffeescript) 写的计算机代数系统。它是一个是一个应用于符号数学的JavaScript库,它尽可能地...免费:基于MIT协议示例和使用说明参见http://algebrite.org/ 标签:Algebrite

    acm之java大数用法

    以下是一个简单的示例,展示了如何使用 `BigInteger`: ```java import java.math.BigInteger; public class BigIntegerExample { public static void main(String[] args) { BigInteger num1 = new BigInteger...

    BigInteger:持有真正的真正的大整数

    在提供的压缩包文件`BigInteger-master`中,可能包含了`BigInteger`类的源代码实现、示例程序、测试用例或者其他相关资源。这可以帮助开发者深入理解`BigInteger`的工作原理,学习如何有效地使用它,并且可能包含了...

Global site tag (gtag.js) - Google Analytics