Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top