Hi there,
I need help with two methods:
One that does the division between the numbers & one that returns the mod
The
program's goal is to do these math operations without using
the Big-Int class from java. That's the reason why
I use Array Lists to do it.
import java.util.*;
public class NumberOperations {
public static void main(String[] args){
ArrayList
ArrayList
ArrayList
//just make the program work with big numbers like this.
//thats
the whole purpose: handeling big numbers.
String
num1 = "123456789101112";
String
num2 = "12345678910";
//I
thought about storing the numbers backwards into the arraylist
//for
a better handeling of the number, feel free to do it as
you pleased.
for(int i = num1.length() -1; i>= 0; i --){
char numberChar = num1.charAt(i);
String
numberString = ""+numberChar;
biggerNumber.add(Integer.parseInt(numberString));
}
System.out.println("Printing num1: "); //
output: [2,1,1,1,0,1,9,8,7,6,5,4,3,2,1]
for(int i = num2.length() -1; i>= 0; i --){
char numberChar = num2.charAt(i);
String
numberString = ""+numberChar;
smallerNumber.add(Integer.parseInt(numberString));
}
System.out.println("Printing num2: "); //output:
[0,1,9,8,7,6,5,4,3,2,1]
// also, assume that the bigger number is always first in
parameters, which is num1.
// and smaller number is second in parameters, which is num2.
operatedNumber = devide(biggerNumber, smallerNumber);
System.out.println("Division of numbers: " + operatedNumber); // dont worry about
the output from this
// or if the number outputs backwards
operatedNumber = mod(biggerNumber, smallerNumber);
System.out.println("Mod of numbers: " + operatedNumber); // dont worry about
the output from this
// or if the number outputs backwards
}
//Division method should look like this:
public static ArrayList
ArrayList
//
//
//
return devidedNumber;
}
//I
also need a method that returns the mod of two numbers.
//This
is basically the same as devision() method except that it returns the mod.
public static ArrayList
ArrayList
//
//
//
return modOfNumbers;
}
}

VulpesPosted Nov 6, 2013, 3:54 PM
I really can't believe that they're expecting you to do that.
A much easier approach is to use repeated subtraction and it's much more plausible that they could expect you to come up with something like the following:
VulpesPosted Jan 13, 2015, 12:20 PM
Mahmoud FaroukPosted Jan 13, 2015, 11:06 AM
SUNIL GUTTAPosted Nov 9, 2013, 1:15 PM
so soo sorry :(
Anyways i bow to you for the efforts and continious guidance in every possible way
Cheers
Have a nice day :) enjoy week-end
VulpesPosted Nov 9, 2013, 10:05 AM
Here it is. Seems to be working fine and quickly whatever size of numbers you feed it. Should also deal with negative numbers, infinities and NaN:
SUNIL GUTTAPosted Nov 9, 2013, 7:11 AM
Dont code . just thrown idea how to do .. i.e the way u used above for loop in awesome way :)
VulpesPosted Nov 8, 2013, 7:37 PM
I haven't time to code this now but I'll have a go tomorrow if I can find time.
SUNIL GUTTAPosted Nov 8, 2013, 2:36 PM
but please
It would be nice if you could give me ideas of an algorithm that would solve division using a loop like you did on your subtract() method above , . . I like the way :)
Cheers
Ty ..
SUNIL GUTTAPosted Nov 8, 2013, 12:36 PM
U already mentioned it will take many iterations & performance will get effected
feed back i got :
Your code actually does the division between two numbers, but if I have a 30+ digit number for example (which exceed the limit of a long), it wont give an answer. The program should be able to work with numbers like those, thats the whole purpose of the program. I specified it in the question.
I personally feel question is some what unclear :)
Cheers . :(
SUNIL GUTTAPosted Nov 6, 2013, 12:28 AM
Actually I need the program above working with numbers even bigger than 19 digits. The program must work with ArrayList to store the number in order to do the math operations with it, not converting to long,
and YES, its kinda writing my own BigInt class. .
Is it possible ?
VulpesPosted Nov 5, 2013, 8:43 AM
If it's just to divide those particular numbers without using the BigInteger, then you can simply convert to the 'long' type which can deal with integers up to 18 or 19 digits long.
If it's to write methods which can divide integers of arbitrary size then, if performance is not a consideration, the easiest way is to use repeated subtraction.
Unless you've been told to do so, I wouldn't use ArrayList