[英]implementing multiplication algorithm in java
我嘗試使用Java實現以下乘法算法:
m:是一個數字
n:是b位數
β:是基數
這是實現此算法的java函數:
public BigInteger prodbigbig(BigInteger a, BigInteger b, Integer base ){
ArrayList<Integer> listA = new ArrayList<Integer>();
ArrayList<Integer> listB = new ArrayList<Integer>();
ArrayList<Integer> listC = new ArrayList<Integer>();
int m = a.toString().length();
int n = b.toString().length();
Integer carry, temp;
BigInteger result = BigInteger.ZERO;
for(int i = 0; i < a.toString().length(); i++){
listA.add(Character.getNumericValue( a.toString().charAt(i)));
}
for(int i = 0; i < b.toString().length(); i++){
listB.add((Character.getNumericValue( b.toString().charAt(i))));
}
for(int i= 0; i <= m - 1; i++){
listC.add(0);
}
for(int k = 0; k <= n - 1 ; k++) {
carry = 0;
for(int i = 0; i <= m - 1; i++){
temp = (listA.get(i) * listB.get(k)) + listC.get(i + k) + carry;
listC.add(i+k,temp % base);
carry = temp / base;
}
listC.add(k + m,carry);
}
for(int i = 0; i < m + n; i++) {
result = result.add(BigInteger.valueOf((long) (listC.get(i)*(Math.pow(base, i)))));
}
return result;
}
但是我不知道為什么我沒有得到正確的結果,直到現在我還無法檢測到它失敗的地方。
listC.add(i+k,temp % base);
那應該是
listC.set(i+k,temp % base);
並且您最終轉換為BigInteger的次數將滿溢。 我將完全擺脫ArrayLists
並使用int
數組,然后在末尾將其轉換為byte[]
並將其直接提供給BigInteger.
的構造函數BigInteger.
看起來您應該在setC中使用set
而不是add
,因為add(index,value)將其他元素移到index之后,但是set
被替換了。
public BigInteger prodbigbig(BigInteger a, BigInteger b, Integer base ){
ArrayList<Integer> listA = new ArrayList<Integer>();
ArrayList<Integer> listB = new ArrayList<Integer>();
ArrayList<Integer> listC = new ArrayList<Integer>();
int m = a.toString().length();
int n = b.toString().length();
Integer carry, temp;
BigInteger result = BigInteger.ZERO;
for(int i = 0; i < a.toString().length(); i++){
listA.add(Character.getNumericValue( a.toString().charAt(i)));
}
for(int i = 0; i < b.toString().length(); i++){
listB.add((Character.getNumericValue( b.toString().charAt(i))));
}
for(int i= 0; i <= m - 1; i++){
listC.add(0);
}
for(int k = 0; k <= n - 1 ; k++) {
carry = 0;
for(int i = 0; i <= m - 1; i++){
temp = (listA.get(i) * listB.get(k)) + listC.get(i + k) + carry;
listC.set(i+k,temp % base);
carry = temp / base;
}
listC.set(k + m,carry);
}
for(int i = 0; i < m + n; i++) {
result = result.add(BigInteger.valueOf((long) (listC.get(i)*(Math.pow(base, i)))));
}
return result;
}
我以這種方式解決了修改代碼的問題:
public BigInteger prodBigBig(BigInteger a, BigInteger b, int base ){
BigInteger result = BigInteger.ZERO;
if(a.compareTo(result) == 0 || b.compareTo(result) == 0)
return result;
int m = a.toString().length();
int n = b.toString().length();
int carry;
int temp;
ArrayList<Integer>listA = new ArrayList<>();
ArrayList<Integer>listB = new ArrayList<>();
int[] listC = new int[m + n];
for(int i = m - 1; i >= 0; i--){
listA.add(Character.getNumericValue( a.toString().charAt(i)));
listC[i] = 0;
}
for(int i = n - 1; i >= 0; i--) {
listB.add(Character.getNumericValue(b.toString().charAt(i)));
}
for(int k=0; k < n; k++) {
carry = 0;
for(int i = 0; i < m ; i++){
temp = (listA.get(i) * listB.get(k)) + listC[i + k] + carry;
listC[i + k] = temp % base;
carry = temp / base;
}
listC[k + m] = carry;
}
for(int i= 0; i < m + n; i++){
result = result.add(BigInteger.valueOf((long) (listC[i] * Math.pow(base, i))));
}
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.