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:
- Add
- multiply
- 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
BigInteger? That's class purpose is exaclty that stuff?3456and93876. 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