gpt4 book ai didi

java - 在 Java 中使用 String 创建我自己的 BigInteger

转载 作者:行者123 更新时间:2023-12-01 13:50:41 24 4
gpt4 key购买 nike

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

抱歉,但乘法方法已修复并且工作正常。但这并不是我类唯一的问题。我使用减法创建了一个 mod 方法。在我的减法方法中,我使用 subAbs 方法,这是两个 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());

}

如果我输入一个很大的数字,我会得到一个异常:enter image description here

问题可能是从subAbs方法开始的。

最佳答案

你的乘法就是简单地一遍又一遍地相加。这对于小数字来说效果很好,但是当您输入大量数字时,您需要进行大量计算,计算机必须花费很长时间才能计算出来。

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

关于java - 在 Java 中使用 String 创建我自己的 BigInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com