CoderFromSpace
CoderFromSpace

Reputation: 49

Divide two large numbers as strings without using Bignumbers in java

I need to divide two large integers WITHOUT using Biginteger since the Numbers can't be stored inside a primitive type , since I need to do it char by char from the strings I am given,I have already created a class called BigNumber, with this class I can:

  1. Add
  2. multiply
  3. compare two strings with large integers inside

Now I only need to implement the Dividing method but I can't get my head around how to do it with two strings instead of one String and an Int, here's what I got so far, it works if the number we are dividing the String by is small enough to be an int

class BigNumber {
    String Number;
BigNumber div(BigNumber other) {
        String result= "";
        String num1= this.Number;
        long Divisor = Integer.parseInt(other.Number);
        int index = 0;
        long NumTemp = num1.charAt(index)-'0';
        while (NumTemp < Divisor){
            NumTemp = NumTemp * 10 +(num1.charAt(index+1) - '0');
            index++;
        }
        while (num1.length()-1 > index){
            result += (NumTemp/Divisor) ;
            NumTemp = (NumTemp % Divisor) * 10 + num1.charAt(index+1) - '0';
            index++;
        }
        result += (NumTemp/Divisor);
        System.out.println(result);
        System.out.println(NumTemp);
        BigNumber Big = new BigNumber(result);
        return Big;
    }
}
`

PS: My class can also subtract one large number to another, if that helps with the division

Upvotes: 2

Views: 3585

Answers (1)

CoderFromSpace
CoderFromSpace

Reputation: 49

I tried what you all told me this morning and got it, thank you all, if you've some improvement over it please tell me, since this is just the rough code without cleaning the inefficiencies, thank you all

BigNumber div(BigNumber other) {
            String result = "";
            String num1 = this.Number;
            String num2 = other.Number;
            int Select = num2.length();
            String temp = num1.substring(0, Select);
            BigNumber tempNum = new BigNumber(temp);
            int NumbersLeft = num1.length() - temp.length();
            BigNumber MultObject = new BigNumber("1");
            if (tempNum.compareTo(other) < 0) {
                temp = num1.substring(0, Select+1);
                tempNum.Number = temp;
                NumbersLeft--;
                Select++;
            }
            do {
                MultObject.Number = "0";
                int Index = 0;
                while (other.mult(MultObject).compareTo(tempNum) < 0) {
                    Index++;
                    MultObject.Number = Integer.toString(Index);
                }
                Index--;
                MultObject.Number = Integer.toString(Index);
                String Carry = tempNum.sub(other.mult(MultObject)).Number;
                if (NumbersLeft > 0) {
                    Select++;
                    Carry += num1.charAt(Select - 1);
                    NumbersLeft--;
                }
                result += Index;
                tempNum.Number = Carry;
            }while (NumbersLeft > 0);
            MultObject.Number = "0";
            int Index = 0;
            while (other.mult(MultObject).compareTo(tempNum) < 0) {
                Index++;
                MultObject.Number = Integer.toString(Index);
            }
            Index--;
            MultObject.Number = Integer.toString(Index);
            String Carry = tempNum.sub(other.mult(MultObject)).Number;
            if (NumbersLeft > 0) {
                Select++;
                Carry += num1.charAt(Select - 1);
                NumbersLeft--;
            }
            result += Index;
            tempNum.Number = Carry;
                BigNumber Big = new BigNumber(result);
                return Big;
            }

Upvotes: 2

Related Questions