0

I have to create a class MyBigInteger to calculate the operations: mod inverse and mod power with >very big integers ( about 60 digits in Decimals or more ). To solve this, I use String to store my >numbers and create some basic functions such as add, subtract, mod, div,... But the problem I got >here is that: while my add and subtract methods work right, my multiple functions only works with >small numbers, and if I use input with numbers 7, 8 or more digits, my program will not responds. I >think my idea to use String to store big numbers may be a bad idea and If i use array to store them, >will my class work more quickly, won't it? Below is my code.The add and subtract method seem to work correctly so I will only post the method >multiple. First is method a MyBigInteger multiply a integer. I use it to create my multipler between two >MyBigInteger:

public class MyBigInteger {
private String val;
    public static final MyBigInteger ZERO = new MyBigInteger("0");
        ...
    private MyBigInteger mutiple( int k){
    MyBigInteger result = ZERO; 
    if( k == 0) return result;
    for( int i = 1; i <= Math.abs(k); i++) result = result.add(this);
    if( k > 0) return result;
    else return result.getOpposite(); // result.add(result.getOpposite()) == ZERO
}


    public MyBigInteger mutiple( MyBigInteger mbi){
    MyBigInteger result = ZERO;
    if( mbi.toString().charAt(0) != '-'){
        for( int i = mbi.toString().length() - 1; i >= 0; i--){
        result =  result.add(this.mutiple(Integer.parseInt(mbi.toString().charAt(mbi.toString().length() - i -1) + "")).mutiple((int)Math.pow(10, i)));
        }
    } else{
        for( int i = mbi.toString().length() - 1 ; i >= 1; i--){
            result = result.add(this.mutiple(Integer.parseInt(mbi.toString().charAt(mbi.toString().length() - i) + "")).mutiple((int)Math.pow(10, i-1)));
        }
        result = result.getOpposite();
    }
    return result;
}

Many thanks for any help you may be able to provide

Sorry for this, but the Multiplication method was fixed and It works perfectly. But that it not the only problem in my Class. I created a mod method by using a subtraction method. And In my subtraction method, I use subAbs method which is a particular subtraction for two Positive MyBigNumber.

public MyBigInteger subAbs( MyBigInteger mBI){      
String result = "";
int i = this.getLength();
int j = mBI.getLength();
int s = 0;
int r = 0;
String temp = "";       
String val1 = this.toString();
String val2 = mBI.toString();
if( this.equalsTo(mBI) == true ) return ZERO;
else
if( this.greaterThan(mBI) == true){         
    for( int k = 0; k < i - j; k++) temp += "0";
    val2 = temp + val2;
    for( int k = i-1; k > 0; k-- ){
     //And the statement right behind this comment is the wrong line (224) in the image
        s = 10 + Integer.parseInt(val1.charAt(k) + "") - Integer.parseInt(val2.charAt(k) + "") - r;             
        if( s >= 10){
            s = s - 10;
            r = 0; 
        } else  r = 1;
        result = Integer.valueOf(s).toString() + result;
    }
    s = Integer.parseInt(val1.charAt(0) + "") - Integer.parseInt(val2.charAt(0)+"") - r;
    if( s >= 0 ) result = s + result;               
    else result = Integer.valueOf(s).toString() + result;   
    return new MyBigInteger(result);
} else return new MyBigInteger("-" + mBI.subAbs(this).toString());

}

And if I put in a big number, I get a exception:enter image description here

The problem may start from the method subAbs.

3
  • 1
    Try StringBuilder instead of String. Commented Nov 14, 2013 at 18:31
  • @PM77-1 thank you, I just read about StringBuilder and will try it now. Commented Nov 14, 2013 at 18:50
  • 1
    You still need more efficient multiplication algorithm. Commented Nov 14, 2013 at 19:00

1 Answer 1

1

Your multiply method is simply adding over and over and over. This works fine for small numbers, but when you put in large numbers you are doing tons of calculations, the computer has to take a long time to figure it out.

How would you multiply 100x12345 by hand? Would you add 12345+12345, then take that and add 12345, then take that and add 12345, and repeat 100 times? That's what your alogirthm is doing now. You should try to implement your multiply algorithm in the same way you would multiply 100x12345.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your very soon and helpful answer. I think I understand what you said. What I have to do is that instead of using addition medthod 100 times, I should only add 2 digits "0" at the end of the string. And this will make my program more quickly., is that right?
That's...not a general way of doing it. Better: how would you multiply 123 x 12345 by hand? Implement that algorithm.
Remember when you learned multiplication of big numbers in school? You can refresh you memory by looking at the following picture: freepatentsonline.com/6633896-0-large.jpg
Thank you so much. I think I nearly forgot such a very basic knowledge. So embarrassing. Thank again for spend time for me.

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.