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

BigInteger使用例子2

阅读更多
import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
  
import java.util.ArrayList;   
  
import java.math.BigInteger;   
  
  
/**  
* This program displays factorials as the user enters values interactively  
*/  
public class FactQuoter {   
  public static void main(String[] args) throws IOException {   
    // This is how we set things up to read lines of text from the user.   
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));   
    // Loop forever   
    for (;;) {   
      // Display a prompt to the user   
      System.out.print("FactQuoter> ");   
      // Read a line from the user   
      String line = in.readLine();   
      // If we reach the end-of-file,   
      // or if the user types "quit", then quit   
      if ((line == null) || line.equals("quit"))   
        break;   
      // Try to parse the line, and compute and print the factorial   
      try {   
        int x = Integer.parseInt(line);   
        System.out.println(x + "! = " + Factorial4.factorial(x));   
      }   
      // If anything goes wrong, display a generic error message   
      catch (Exception e) {   
        System.out.println("Invalid Input");   
      }   
    }   
  }   
}   
  
/**  
* 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对象`num1`和`num2`,然后分别进行了加法、减法和乘法操作,并打印了结果。 **应用场景:** 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` ...

    大数的 加 减 乘 除 开方运算Java版

    这些示例展示了如何在Java中使用`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...

Global site tag (gtag.js) - Google Analytics