2

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

13
  • 5
    I don't get why you need to use chars and not BigIntegers... Commented Feb 2, 2018 at 12:25
  • 1
    Why can't you use BigInteger? That's class purpose is exaclty that stuff? Commented Feb 2, 2018 at 12:25
  • 1
    You are doing decimal arithmetic, so shift the divisor by adding zeroes and subtract. Repeat on the remainder. Commented Feb 2, 2018 at 12:31
  • So they're not signed? Commented Feb 2, 2018 at 12:51
  • 2
    Take a pencil and paper and two largeish numbers, maybe 3456 and 93876. Using the method for long division you were taught in school divide the former into the latter. Pay careful attention to the algorithm you use, in fact pay such careful attention you can write down each individual step you take. Voila Commented Feb 2, 2018 at 13:50

1 Answer 1

2

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;
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.