How to add two numbers of any length in java?

后端 未结 8 1151
广开言路
广开言路 2020-12-06 01:20

How to add two numbers of any length in java?

Say for example, in java long size is 64 bit. So the maximum range is -9223372036854775808 to 9223372036854775807. Am i

相关标签:
8条回答
  • 2020-12-06 01:48
    public class AddNumbers {
        public static void main(String args[]) {
            String a = new String("3999988889999999995555558888999444333333333222229998877666555444888888");
            String b = new String("56867865876989679765465456412332199");
            int loop1 = 0;
            int loop2 = 0;
            StringBuilder sum = new StringBuilder("");
            int carry = 0;
            for (loop1 = a.length() - 1, loop2 = b.length() - 1; loop1 >= 0 || loop2 >= 0; loop1--, loop2--) {
                int indiv1 = 0;
                if (loop1 >= 0)
                    indiv1 = Integer.parseInt("" + a.charAt(loop1));
                int indiv2 = 0;
                if (loop2 >= 0)
                    indiv2 = Integer.parseInt("" + b.charAt(loop2));
                int summation = indiv1 + indiv2 + carry;
                double d = Math.floor(summation / 10);
                carry = (int) d;
                int sum2 = summation % 10;
                sum.append(sum2);
            }
            System.out.println(sum.reverse());
        }
    }
    
    0 讨论(0)
  • 2020-12-06 01:51

    The BigInteger will let you work with numbers of any size, but you lose a considerable amount of performance over long or int.

    Actually, if you just need to run this operation once (user enters two numbers, and gets the result back), using BigInteger is fine. But if you need to perform the addition operation many times, you could use really your own implementation of big integer. When I was competing in ACM matches, we often used our own implementations based on char arrays (in C++). I suggest the following code. It is assumed that there are two arrays of integers, A and B. A[0] and B[0] store the lens of the corresponding numbers. A[i] and B[i] stores the digits themselves. A[1] and B[1] are the least significant digits. Therefore the number 1234 would correspond to such an array: {4,4,3,2,1}.

    Now, suppose we want to sum these numbers and store them in array C in the same format. Here is an example of code, that you could use:

    int len1 = A[0],  len2 = B[0], divisor = 0;
    int len = len1 >= len2 ? len1 : len2;
    for (int i=1;i<=len;i++) {
      if (i>len1) C[i] = B[i]+divisor;
      else if (i>len2) C[i] = A[i]+divisor;
      else C[i] = A[i]+B[i]+divisor;
      divisor = C[i]/10;
      C[i] %= 10;
    }
    while (divisor>0) {
      C[++len] = divisor%10;
      divisor /= 10;
    }
    C[0] = len;
    

    That code uses the simple rules of arithmetic addition and should work significantly faster than the BigInteger general implementation. Have fun with using that.

    0 讨论(0)
  • 2020-12-06 01:53

    Check out the BigInteger class. It will be able to perform the operations you are looking for on really large numbers.

    http://download.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html

    0 讨论(0)
  • 2020-12-06 01:57

    Use BigInteger. Here is an example.

    Example code (based on above link) -

    BigInteger reallyBig1 = new BigInteger("1234567890123456890");
    BigInteger reallyBig2 = new BigInteger("2743534343434361234");
    reallyBig = reallyBig.add(reallyBig2);
    
    0 讨论(0)
  • 2020-12-06 01:57

    Is there any big difference between the addition of two very large numbers using BigInteger and the method, i specified above (add each character from end and store remainder in temporary variable and goes on).

    The difference is that you could use a larger radix, for example. Suppose the radix is 10000, not just 10. When the code of my previous answer would be modified like this:

    int len1 = A[0],  len2 = B[0], divisor = 0;
    int len = len1 >= len2 ? len1 : len2;
    
    for (int i=1;i<=len;i++) {
      if (i>len1) C[i] = B[i]+divisor;
      else if (i>len2) C[i] = A[i]+divisor;
      else C[i] = A[i]+B[i]+divisor;
      divisor = C[i]/10000;
      C[i] %= 10000;
    }
    while (divisor>0) {
      C[++len] = divisor%10000;
      divisor /= 10000;
    }
    C[0] = len;
    

    In that case the code runs 4 time faster (since there is no difference for the virtual machine in arithmetic operations, since they depend on the constant only). Also, this means, that the array of integers will be 4 times smaller. The only problem this causes is how to format the output.

    0 讨论(0)
  • 2020-12-06 01:59

    You can use a BigInteger.

    BigInteger a = new BigInteger("9223372036854775807");
    BigInteger b = new BigInteger("9223372036854775808");
    BigInteger result = a.add(b);
    

    The BigInteger will let you work with numbers of any size, but you lose a considerable amount of performance over long or int.

    0 讨论(0)
提交回复
热议问题