How can I store a number with 10^6 digits in Java?
Would BigInteger be the most appropriate while considering performance issues like memory and speed

Recommended Answers

All 19 Replies

Just declaring the variable as int is enough in this case since range of int is 2^32(=4294967296) which is far more than 10^6.

Refer range of primitive data types once.

harinath, he is looking for how to store a number with 10^6 digits, not 10^6 itself.

Oh...shocked.. I think i dint read the question correctly.. I thought it is 10^6 number to be stored...

Is the OP really asking about 10^6 digits to be stored ..? Ohh my god..

Then , i think it looks like quite impractical to me.
He can store that as a string but processing or performing operations on such very large number is not possible.

only Database can do that with real performance

Member Avatar for hfx642

First of all, even a database can't handle 1,000,000 digits for a number.
Second of all, why would ANYONE need a NUMBER with 1,000,000 digits?
(And DON'T tell me pi!!!)
For numbers, you have to consider the number of significant digits.
If you REALLY NEED 1,000,000 digits, use a string.

Hmm... How about using "char" array of size 10^6? A char is size of byte which should be more than enough to hold that many digits?

Hmm... How about using "char" array of size 10^6? A char is size of byte which should be more than enough to hold that many digits?

A Java char is 2 bytes.

Yes, and that should be more than enough? What you need to do is to deal with operation.

// for example
class MyMillionDigit {
  private char[] digits;

  // constructor1
  public MyMillionDigit() {
    digits = new char[1000000];
    ...
  }

  // constructor2
  public MyMillionDigit(String number) {
    digits = new char[1000000];
    ...
  }
  ...
  ...
}

Wouldn't that be enough to handle it? Create other method to deal with mathematics or anything you want to deal with the number. Each char could be saved as '0' ~ '9' or even real integer 0~9.

BigInteger holds arbitrarily large integers as binary values (the implementation uses an array of ints to provide whatever number of bits are required). I can't imagine that there would be any faster way to hold or do arithmetic on giant numbers on an ordinary computer, and certainly it's the most memory-efficient representation.
The only exception may be if you want to work with individual digits of the decimal representation, in which case a byte array with one decimal digit per byte may be better (two decimal digits per byte if memory usage is more important than execution speed/code simplicity).

I don't think the OP is talking about performance here. I think OP asks for a way to hold a number with that many digits?

He did say "...considering performance issues like memory and speed".

Oh never mind. :) Anyway, yes it is a trade of between space & speed.

Thank You everybody for answering the question

BigInteger holds arbitrarily large integers as binary values (the implementation uses an array of ints to provide whatever number of bits are required). I can't imagine that there would be any faster way to hold or do arithmetic on giant numbers on an ordinary computer, and certainly it's the most memory-efficient representation.
The only exception may be if you want to work with individual digits of the decimal representation, in which case a byte array with one decimal digit per byte may be better (two decimal digits per byte if memory usage is more important than execution speed/code simplicity).

Thank You James Cherrill.
I needed it for Cryptographic operations ....RSA basically

The number of atoms in the observable universe is about 10^80. That's 100000000000000000000000000000000000000000000000000000000000000000000000000000000. That's about 80 digits. I'm not sure what one might be counting that you would need 1000000 decimal digits to count them. ;->

So: BigInteger can do it.

Also, assuming that there are a much smaller number of *significant* digits, 'double' or even 'float' would be good choices.

Also you could do something like:

public class LargeNum { //define class
    int[] num = new int[1000000]; //declare int array with 10^6 "entries"
    public LargeNum(String number) { //define constructor(?)
        char[] ca = number.toCharArray(); //declare temporary char array to hold numbers
        for (int i = 0; i > 1000000; i++) { //create a loop
            num[i] = Integer.parseInt(ca[i].toString()); //isn't that how you convert String to int? (Move ca to num)
        } //end for
    } //end constructor(?)
} //end class

and you could use num. (AFAIK)

Also you could do something like:...

Hi Delocaz. Yes you could.
And here are a couple of suggestions as to how that code could be optimised to reduce memory usage, especially if there a number of instances of this class:
1. The array num is going to be quite big. It uses an int (32 bits) per element, but the values in each will just be 0-9. Better to make it a byte array, just 8 bits per element.
2. number.toCharArray(); will create another large array, which you can avoid by using number.charAt(i) to get the chars directly from the original String.
3. You can use parseInt to convert String to int, but in this particular case we need to convert individual chars which we know contain only '0' thru '9', so you can use the fact that char is an integer type as follows: num[i] = number.charAt(i) - '0'; Please don't take these as criticisms, there's nothing wrong with your code, they are just intended as helpful suggestions.

A 1000000 digit number, in ASCII is, of course, about a megabyte. As a Java Unicode string, two megabytes. As a BigInteger, a bit over 400K bytes. A double is 8 bytes, and a float 4 bytes.

So unless you have a lot of significant digits, a floating point representation probably makes a lot of sense.

... unless you have a lot of significant digits, a floating point representation probably makes a lot of sense.

For practical/physics purposes, yes. But this is apparently for "Cryptographic operations" so presumably every digit is significant - but maybe the OP can confirm that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.