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.

4

1 回答 1

1

您的乘法方法只是一遍又一遍地添加。这适用于小数字,但是当您输入大量数字时,您需要进行大量计算,计算机必须花费很长时间才能计算出来。

你将如何手动乘以 100x12345?你会加 12345+12345,然后拿那个加 12345,然后拿那个加 12345,重复 100 次吗?这就是你的算法现在正在做的事情。您应该尝试以与乘以 100x12345 相同的方式实现乘法算法。

于 2013-11-14T18:32:21.767 回答