Search code examples
javadivide-and-conquermultiplication

Multiplying two decimal integer with divide and conquer java


I'm trying to multiply two numbers which they're positive integer and they have same number of digits,with divide and conquer recursively,i'm trying to do it something like that: T(n)=4T(n/2)+O(n) note:i know that it runs in theta(n^2),and it's terrible!it's just a exercise for me. thank you,and sorry for my bad english. :) and my question:where is my mistake? algorithm based on this doc here's the code:

import java.util.Scanner;
public class main {
static int res=0;
static int stage =0;
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    char[] Num1;
    char[] Num2;
    String num1 = in.nextLine();
    String num2 = in.nextLine();
    in.close();
    Num1 = num1.toCharArray();
    Num2 = num2.toCharArray();



    DaQMultiplay(Num1, Num2);
    System.out.println(res);
}
static int DaQMultiplay(char[] num1,char[] num2){
    if(num1.length<2){
        stage++;
        int num1sd =Integer.parseInt(new String(num1));
        int num2sd =Integer.parseInt(new String(num2));
        return (num1sd*num2sd);
    }
    stage++;
    double len = num1.length;
    int lenl = (int) Math.ceil(len/2);
    char []ln1 = new char[lenl];
    char []rn1 = new char[(int) (len-lenl)];
    char []ln2 = new char[lenl];
    char []rn2 = new char[(int) (len-lenl)];
    for (int i = 0; i < ln1.length; i++) {
        ln1[i]=num1[i];
    }
    for (int i = 0; i < rn1.length; i++) {
        rn1[i]=num1[i+lenl];
    }
    for (int i = 0; i < ln2.length; i++) {
        ln2[i]=num2[i];
    }
    for (int i = 0; i < rn2.length; i++) {
        rn2[i]=num2[i+lenl];
    }
    System.out.print("Left Side of num1:"+stage+" ");
    System.out.println(ln1);

    System.out.print("Right Side of num1:"+stage+" ");
    System.out.println(rn1);

    System.out.print("Left Side of num2:"+stage+" ");
    System.out.println(ln2);

    System.out.print("Right Side of num2:"+stage+" ");
    System.out.println(rn2);


    res+=DaQMultiplay(ln1,ln2)*(10^((int)len));
    System.out.println("res: "+res);
    res+=DaQMultiplay(ln1,rn2)*10^((int) (len-lenl));
    System.out.println("res: "+res);
    res+=DaQMultiplay(rn1,ln2)*10^((int) (len-lenl));
    System.out.println("res: "+res);
    res+=DaQMultiplay(rn1, rn2);
    System.out.println("res: "+res);
    return 0;
}
}

output: for num1=20011,num2=91281

    20011
91281
Left Side of num1:1 200
Right Side of num1:1 11
Left Side of num2:1 912
Right Side of num2:1 81
Left Side of num1:2 20
Right Side of num1:2 0
Left Side of num2:2 91
Right Side of num2:2 2
Left Side of num1:3 2
Right Side of num1:3 0
Left Side of num2:3 9
Right Side of num2:3 1
res: 144
res: 164
res: 164
res: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at main.DaQMultiplay(main.java:46)
at main.DaQMultiplay(main.java:63)
at main.DaQMultiplay(main.java:61)
at main.main(main.java:19)    

Solution

  • here is the complete program and everything is working fine:

    import java.io.ObjectInputStream.GetField;
    import java.util.Scanner;
    
    public class main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            char[] Num1;
            char[] Num2;
            String num1 = in.nextLine();
            String num2 = in.nextLine();
            in.close();
            Num1 = num1.toCharArray();
            Num2 = num2.toCharArray();  
            check(Num1, Num2);
        }
        static int DaQMultiplay(char[] num1,char[] num2){
            int res=0;
            if(num1.length<=1){
                int num1sd = 0;
                int num2sd = 0;
                if(num1!=null && !num1.equals("") && new String(num2).trim().length()>0){
                    num1sd =Integer.parseInt(new String(num1));
                }
    
                if(num2!=null && !num2.equals("") && new String(num2).trim().length()>0){
                    num2sd=Integer.parseInt(new String(num2));
                }
                return (num1sd*num2sd);
            }
            double len = num1.length;
            int lenl = (int) Math.ceil(len/2);
            char []ln1 = new char[lenl];
            char []rn1 = new char[(int) (len-lenl)];
            char []ln2 = new char[lenl];
            char []rn2 = new char[(int) (len-lenl)];
            for (int i = 0; i < ln1.length; i++) {
                ln1[i]=num1[i];
            }
            for (int i = 0; i < rn1.length; i++) {
                rn1[i]=num1[i+lenl];
            }
            for (int i = 0; i < ln2.length; i++) {
                ln2[i]=num2[i];
            }
            for (int i = 0; i < rn2.length; i++) {
                if(num2.length>i+lenl){
                    rn2[i]=num2[i+lenl];
                }
            }
    
            res+=(DaQMultiplay(ln1,ln2)*(Math.pow(10, len)));
            res+=(DaQMultiplay(rn1,ln2)*(Math.pow(10, (len/2))));
            res+=(DaQMultiplay(ln1,rn2)*(Math.pow(10, (len/2))));
            res+=(DaQMultiplay(rn1, rn2));
    
            return res;
        }
        static void check(char []Num1,char []Num2){
            int res=0;
            if ((Num1.length%2)==0) {
                res=DaQMultiplay(Num1, Num2);
                System.out.println(res);
            }
            else{
                String num1 = String.valueOf(Num1);
                num1 = num1+"0";
                String num2 = String.valueOf(Num2);
                num2 = num2+"0";
                Num1=num1.toCharArray();
                Num2=num2.toCharArray();
                res=DaQMultiplay(Num1, Num2);
                res=(res/100);
                System.out.println(res);
            }
        }
    }