1

I have implemented a solutions for multiplying any two large numbers, the solution I am getting seems to be working fine for many inputs however when I pass

"3141592653589793238462643383279502884197169399375105820974944592" "2718281828459045235360287471352662497757247093699959574966967627"

It's not giving correct answer

I am using Basic Java Syntax

import java.util.*;

public class GenericOperations {


    static String addTwoLargeNumber(String X, String Y)
    {
        String Z = "";
        int i = X.length() - 1;
        int j = Y.length() - 1;
        int carry = 0;
        while(i>-1 && j>-1)
        {
            int digitSum =  Character.getNumericValue(X.charAt(i)) + 
                            Character.getNumericValue(Y.charAt(j)) + carry; 
            if(digitSum > 9)
            {
                Z =  digitSum%10 + Z;
                carry=1;
            }
            else
            {
                Z =  digitSum + Z;
                carry = 0;
            }
            i--;j--;
        }
        while(i>-1)
        {
            int digitSum = Character.getNumericValue(X.charAt(i)) + carry;
            if(digitSum > 9)
            {
                Z =  digitSum%10 + Z;
                carry=1;
            }
            else
            {
                Z =  digitSum + Z;
                carry = 0;
            }
            i--;

        }
        while(j>-1)
        {
            int digitSum = Character.getNumericValue(Y.charAt(j)) + carry;
            if(digitSum > 9)
            {
                Z =   digitSum%10 + Z;
                carry=1;
            }
            else
            {
                Z =  digitSum + Z ;
                carry = 0;
            }
            j--;

        }

        return Z;
    }

    static String multiplyTwoLargeNumbers(String X, String Y)
    {
        ArrayList<String> additionList = new ArrayList<String>();
        ArrayList<String> additionListWithZeros = new ArrayList<String>();
        int n = X.length() - 1;
        int m = Y.length() - 1 ;
        int carry = 0;
        int i=0,j=0;

        for(i=n ; i>-1; i--)
        {
            carry = 0;
            String Z = "";

            for(j = m ; j>-1 ; j--)
            {



                int digitSum =  (   Character.getNumericValue(X.charAt(i)) * 
                                    Character.getNumericValue(Y.charAt(j))  ) 
                                + carry; 
                if(digitSum > 9)
                {
                    Z =  digitSum%10 + Z;
                    carry= digitSum/10;

                }
                else
                {
                    Z =  digitSum + Z;
                    carry = 0;
                }
            }

            if(carry!=0)
            {
                Z =  carry + Z;
            }
            additionList.add(Z);
        }

        int k = 0;
        String sum = "";
        for (Iterator<String> iterator = additionList.iterator(); iterator.hasNext();) 
        {

            String val1 = iterator.next();
            for(int x = 0;x<k;x++)
            {
                val1 = val1 + 0;
            }
            k++;   
            //System.out.println(val1);
            sum = addTwoLargeNumber(sum,val1);
        }



        return sum;

    }

    static void printArray(int arr[]) 
    { 
        int n = arr.length; 
        for (int i=0; i<n; ++i)
        {
            System.out.print(arr[i] + " ");
        }
        System.out.println(); 
    }


    public static void main(String args[])
    {
        String val = multiplyTwoLargeNumbers(
        "3141592653589793238462643383279502884197169399375105820974944592",
        "2718281828459045235360287471352662497757247093699959574966967627");

        System.out.println(val);
    }
}

Expected Result:

8539734222673567065463550869546574495034888535765114961879601127067743044893204848617875072216249073013374895871952806582723184

Actual Results:

8539734222673566965463549869546574495034887534765114961879601127067743044893204848617875072216249073013374895871952806582723184

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
akatira
  • 21
  • 2
  • maybe I missed it, but I don't see a difference in your expected and actual results – Stultuske May 06 '19 at 10:34
  • 8539734222673567*** in Expected, 8539734222673566*** in Actual – akatira May 06 '19 at 10:35
  • 3
    Why are you implementing this algorithm? Maybe it is homework? You should use java.math.BigDecimal to operate such big numbers. – Oscar Pérez May 06 '19 at 10:37
  • 2
    @OscarPérez In that case, `BigInteger` would do. – assylias May 06 '19 at 10:38
  • Not a homework but can say something inspired from it. You can find a legit solution on GeeksForGeeks or any portal, I just want to know why above solution is wrong. What is the logical error that I have missed. – akatira May 06 '19 at 10:40
  • max value of integer - [stackoverflow.com/questions/15004944/max-value-of-integer](https://stackoverflow.com/questions/15004944/max-value-of-integer) – edjm May 06 '19 at 10:42
  • 1
    I appreciate your suggestion, However I want to find out the Logical error here. And not change the implementation. – akatira May 06 '19 at 10:46
  • 1
    @akatira Your algo fails on 99 * 11 or 11 * 91. That should be reasonably easy to debug. – assylias May 06 '19 at 10:52

1 Answers1

2

You just missed a carry when adding.

Add following and you should be done:

    if (carry != 0) {
        Z = carry + Z;
    }

BTW: simplified the addTwoLargeNumber-method

static String addTwoLargeNumber(String X, String Y) {
    String Z = "";
    int maxPos = Math.max(X.length(), Y.length());
    int carry = 0;
    for (int pos = 0; pos < maxPos; pos++) {
        final int currValX
                = pos < X.length()
                ? Character.getNumericValue(X.charAt(X.length() - 1 - pos)) : 0;
        final int currValY
                = pos < Y.length()
                ? Character.getNumericValue(Y.charAt(Y.length() - 1 - pos)) : 0;
        int digitSum = currValX + currValY + carry;
        Z = digitSum % 10 + Z;
        carry = digitSum / 10;
    }
    if (carry != 0) {
        Z = carry + Z;
    }
    return Z;
}
SirFartALot
  • 1,215
  • 5
  • 25
  • Thanks a lot, Was too much focused on multiplication function didn't realize that something was wrong in the Addition – akatira May 06 '19 at 11:05