Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when you need to store a (very) large number?

I am trying to do a Project Euler problem but it involves adding the digits of a very large number. (100!)

Using Java int and long are too small.

Thanks for any suggestions

like image 366
irl_irl Avatar asked Mar 27 '09 03:03

irl_irl


People also ask

Which data structure can store very large numbers?

Which among the following data structures is best suited for storing very large numbers (numbers that cannot be stored in long long int). Following are the operations needed for these large numbers. Explanation: The only two choices that make sense are Array and Linked List.

How do you handle large numbers in C++?

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications.


3 Answers

Class BigInteger looks like it might be what you are looking for.

like image 122
Himadri Choudhury Avatar answered Sep 24 '22 16:09

Himadri Choudhury


Use BigInteger. Here is an example from the book Java Examples in a Nutshell that involves computing factorials, with caching.

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));
  }
}
like image 20
William Brendel Avatar answered Sep 20 '22 16:09

William Brendel


java.lang.BigInteger or java.lang.BigDecimal

like image 43
Martin Beckett Avatar answered Sep 22 '22 16:09

Martin Beckett