java biginteger转int_如何在不使用java.math.BigInteger的情况下使用Java处理非常大的数字...
我認為程序員應該已經實現了自己的bignum庫,因此歡迎在這里。
(當然,稍后您會發現BigInteger更好,并且可以使用它,但這是寶貴的學習經驗。)
(您可以在github上關注本課程的源代碼。此外,我將此內容(略有修飾)重新制成了一個由14部分組成的博客系列。)
用Java創建一個簡單的Big number類
那么,我們需要什么?
首先,用數字表示
基于Java給我們的數據類型。
您認為十進制轉換是最復雜的部分,讓我們停留在基于十進制的模式下。為了提高效率,我們將不存儲真實的十進制數字,而是使用base 1 000 000 000 = 10^9 < 2^30。這適合于Java int(最大為2^31或2^32),而兩個這樣的數字的乘積恰好適合于Java long。
final static int BASE = 1000000000;
final static int BASE_DECIMAL_DIGITS = 9;
然后是數字數組:
private int[] digits;
我們是否將數字存儲在小端或大端中,即較大的部分在前還是后?這并不重要,因此我們決定選擇大端字節,因為這是人類想要閱讀的方式。(目前,我們專注于非負值-稍后我們將為負數添加一個符號位。)
為了進行測試,我們添加了一個構造函數,該構造函數允許從此類int []進行初始化。
/**
* creates a DecimalBigInt based on an array of digits.
* @param digits a list of digits, each between 0 (inclusive)
*? ? and {@link BASE} (exclusive).
* @throws IllegalArgumentException if any digit is out of range.
*/
public DecimalBigInt(int... digits) {
for(int digit : digits) {
if(digit < 0 ||? BASE <= digit) {
throw new IllegalArgumentException("digit " + digit +
" out of range!");
}
}
this.digits = digits.clone();
}
另外,此構造函數還可以用于單個int(如果小于BASE),甚至不可以int(我們將其解釋為0)。因此,我們現在可以執行以下操作:
DecimalBigInt d = new DecimalBigInt(7, 5, 2, 12345);
System.out.println(d);
這給了我們de.fencing_game.paul.examples.DecimalBigInt@6af62373,而不是那么有用。因此,我們添加了一個toString()方法:
/**
* A simple string view for debugging purposes.
* (Will be replaced later with a real decimal conversion.)
*/
public String toString() {
return "Big" + Arrays.toString(digits);
}
現在的輸出是Big[7, 5, 2, 12345],這對于測試更有用,不是嗎?
第二,從十進制格式轉換。
我們在這里很幸運:我們的基數(10 ^ 9)是我們要從(10)轉換的基數的冪。因此,我們總是有相同的(9)個十進制數字代表一個“我們的格式”數字。(當然,開始時可能少一些數字。)在下面的代碼中,decimal是一個十進制數字的字符串。
int decLen = decimal.length();
int bigLen = (decLen-1) / BASE_DECIMAL_DIGITS + 1;
這個奇怪的公式是Java int的編寫方式? bigLen = ceil(decLen/BASE_DECIMAL_DIGITS)。(我希望它是正確的,我們稍后將對其進行測試。)
int firstSome = decLen - (bigLen-1) * BASE_DECIMAL_DIGITS;
這是第一位十進制數字的長度,應在1到9(含)之間。
我們創建數組:
int[] digits = new int[bigLen];
循環瀏覽要創建的數字:
for(int i = 0; i < bigLen ; i++) {
我們的每個數字都由原始數字中的一個數字塊表示:
String block =
decimal.substring(Math.max(firstSome + (i-1)*BASE_DECIMAL_DIGITS, 0),
firstSome +? ?i? *BASE_DECIMAL_DIGITS);
(Math.max這里是第一個較短的塊在這里需要的。)現在,我們使用常規的Integer解析函數,并將結果放入數組中:
digits[i] = Integer.parseInt(block);
}
從現在創建的數組中,我們創建DecimalBigInt對象:
return new DecimalBigInt(digits);
讓我們看看這是否有效:
DecimalBigInt d2 = DecimalBigInt.valueOf("12345678901234567890");
System.out.println(d2);
輸出:
Big[12, 345678901, 234567890]
看起來不錯:-)我們也應該用其他一些數字(長度不同)對其進行測試。
下一部分將是十進制格式,這應該更加容易。
第三,轉換為十進制格式。
我們需要將每個數字輸出為9個十進制數字。為此,我們可以使用Formatter支持類printf格式字符串的類。
一個簡單的變體是這樣的:
public String toDecimalString() {
Formatter f = new Formatter();
for(int digit : digits) {
f.format("%09d", digit);
}
return f.toString();
}
對于我們的兩個數字,將返回000000007000000005000000002000012345和000000012345678901234567890。這適用于往返(即,將其饋送到valueOf方法中會得到一個等效的對象),但前導零看起來并不太好看(并且可能與八進制數產生混淆)。因此,我們需要打破我們美麗的for-each循環,并使用不同的格式字符串作為前兩位。
public String toDecimalString() {
Formatter f = new Formatter();
f.format("%d", digits[0]);
for(int i = 1 ; i < digits.length; i++) {
f.format("%09d", digits[i]);
}
return f.toString();
}
加成。
讓我們從加法開始,因為它很簡單(以后我們可以將其中的一部分用于乘法)。
/**
* calculates the sum of this and that.
*/
public DecimalBigInt plus(DecimalBigInt that) {
...
}
我希望你能喜歡閱讀,你會讀的公式,從而方法名plus,minus,times來代替add,subtract,multiply。
那么,加法是如何工作的?它的工作原理與我們在學校學習到的十進制數字大于9時相同:加上相應的數字,如果對于某些數字,結果大于10(或BASE在我們的情況下),則將一位帶到下一位數字。這可能會導致所得數字比原始數字多一位。
首先,我們來看一個簡單的例子,即兩個數字具有相同的數字位數。然后看起來就像這樣:
int[] result = new int[this.digits.length];
int carry = 0;
for(int i = this.digits.length-1; i > 0; i--) {
int digSum = carry + this.digits[i] + that.digits[i];
result[i] = digSum % BASE;
carry = digSum / BASE;
}
if(carry > 0) {
int[] temp = new int[result.length + 1];
System.arraycopy(result, 0, temp, 1, result.length);
temp[0] = carry;
result = temp;
}
return new DecimalBigInt(result);
(我們從右到左移動,因此我們可以將任何溢出都帶到下一位數字。如果我們決定使用Little Endian格式,這會有些漂亮。)
如果兩個數字的位數不同,則會變得更加復雜。
為了使其盡可能簡單,我們將其分為幾種方法:
此方法將一位數字添加到數組中的元素(可能已經包含一些非零值),并將結果存儲回數組中。如果有溢出,則通過遞歸調用將其帶到下一個數字(索引少一個,而不是一個多)。這樣我們可以確保數字始終保持在有效范圍內。
/**
* adds one digit from the addend to the corresponding digit
* of the result.
* If there is carry, it is recursively added to the next digit
* of the result.
*/
private void addDigit(int[] result, int resultIndex,
int addendDigit)
{
int sum = result[resultIndex] + addendDigit;
result[resultIndex] = sum % BASE;
int carry = sum / BASE;
if(carry > 0) {
addDigit(result, resultIndex - 1, carry);
}
}
下一個對要添加的整個數字數組執行相同的操作:
/**
* adds all the digits from the addend array to the result array.
*/
private void addDigits(int[] result, int resultIndex,
int... addend)
{
addendIndex = addend.length - 1;
while(addendIndex >= 0) {
addDigit(result, resultIndex,
addend[addendIndex]);
addendIndex--;
resultIndex--;
}
}
現在我們可以實現我們的plus方法:
/**
* calculates the sum of this and that.
*/
public DecimalBigInt plus(DecimalBigInt that) {
int[] result = new int[Math.max(this.digits.length,
that.digits.length)+ 1];
addDigits(result, result.length-1, this.digits);
addDigits(result, result.length-1, that.digits);
// cut of leading zero, if any
if(result[0] == 0) {
result = Arrays.copyOfRange(result, 1, result.length);
}
return new DecimalBigInt(result);
}
如果可以在可能發生溢出的情況下先查看,然后再創建一個比必要的數組大的數組,我們可以在這里做得更好。
啊,一個測試:d2.plus(d2)給出Big[24, 691357802, 469135780],看起來不錯。
乘法。
讓我們記得回到學校時,我們如何在紙上相乘更大的數字?
123 * 123
----------
369? ?<== 123 * 3
246? ? <== 123 * 2
123? ? ?<== 123 * 1
--------
15129
因此,我們必須將第一個數字的每個數字[i]與第二個數字的每個數字[j]相乘,并將乘積加到結果的數字[i + j]中(并注意攜帶)。當然,這里的索引是從右開始而不是從左開始計數。 (現在,我真的希望我使用低端數字。)
由于我們兩個數字的乘積可能超出的范圍int,因此我們使用long乘法。
/**
* multiplies two digits and adds the product to the result array
* at the right digit-position.
*/
private void multiplyDigit(int[] result, int resultIndex,
int firstFactor, int secondFactor) {
long prod = (long)firstFactor * (long)secondFactor;
int prodDigit = (int)(prod % BASE);
int carry = (int)(prod / BASE);
addDigits(result, resultIndex, carry, prodDigit);
}
現在我們可以看到為什么我聲明我的addDigits方法采用resultIndex參數了。(并且我只是將最后一個參數更改為varargs參數,以便能夠在此處更好地編寫。)
因此,這里是交叉乘法的方法:
private void multiplyDigits(int[] result, int resultIndex,
int[] leftFactor, int[] rightFactor) {
for(int i = 0; i < leftFactor.length; i++) {
for(int j = 0; j < rightFactor.length; j++) {
multiplyDigit(result, resultIndex - (i + j),
leftFactor[leftFactor.length-i-1],
rightFactor[rightFactor.length-j-1]);
}
}
}
我希望我的索引計算正確。如果使用小尾數表示法,那就multiplyDigit(result, resultIndex + i + j, leftFactor[i], rightFactor[j])更清楚了,不是嗎?
times現在,我們的方法只需分配結果數組,調用multiplyDigits并包裝結果。
/**
* returns the product {@code this × that}.
*/
public DecimalBigInt times(DecimalBigInt that) {
int[] result = new int[this.digits.length + that.digits.length];
multiplyDigits(result, result.length-1,
this.digits, that.digits);
// cut off leading zero, if any
if(result[0] == 0) {
result = Arrays.copyOfRange(result, 1, result.length);
}
return new DecimalBigInt(result);
}
對于測試,d2.times(d2)給出Big[152, 415787532, 388367501, 905199875, 19052100],這與我的Emacs calc在此處計算的結果相同。
比較方式
我們希望能夠比較我們的兩個對象。因此,我們實現了Comparable它的compareTo方法。
public int compareTo(DecimalBigInt that) {
如何知道我們的一個數字是否大于另一個?首先,我們比較數組的長度。由于我們注意不要引入任何前導零(是嗎?),因此較長的數組應具有較大的數字。
if(this.digits.length < that.digits.length) {
return -1;
}
if (that.digits.length < this.digits.length) {
return 1;
}
如果長度相同,我們可以按元素進行比較。由于我們使用big endian(即big end首先出現),因此我們從頭開始。
for(int i = 0; i < this.digits.length; i++) {
if(this.digits[i] < that.digits[i]) {
return -1;
}
if(that.digits[i] < this.digits[i]) {
return 1;
}
}
如果一切都相同,那么顯然我們的數字是相同的,我們可以返回0。
return 0;
}
equals + hashCode()
每一個良好的不可變類應該實現equals()和hashCode()在合適(兼容)的方式。
對于我們的hashCode(),我們只需對數字進行求和,然后將它們乘以小質數,以確保數字切換不會產生相同的哈希碼:
/**
* calculates a hashCode for this object.
*/
public int hashCode() {
int hash = 0;
for(int digit : digits) {
hash = hash * 13 + digit;
}
return hash;
}
在該equals()方法中,我們可以簡單地委托給compareTo方法,而不必再次實現相同的算法:
/**
* compares this object with another object for equality.
* A DecimalBigInt is equal to another object only if this other
* object is also a DecimalBigInt and both represent the same
* natural number.
*/
public boolean equals(Object o) {
return o instanceof DecimalBigInt &&
this.compareTo((DecimalBigInt)o) == 0;
}
所以,今天足夠了。減法(可能是負數)和除法更加復雜,因此我暫時將其省略。要計算90的階乘,就足夠了。
計算大階乘:
這里的階乘函數:
/**
* calculates the factorial of an int number.
* This uses a simple iterative loop.
*/
public static DecimalBigInt factorial(int n) {
DecimalBigInt fac = new DecimalBigInt(1);
for(int i = 2; i <= n; i++) {
fac = fac.times(new DecimalBigInt(i));
}
return fac;
}
這給了我們
fac(90) = 1485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000
從任意基數表示形式轉換
在下一個frodosamoa問題的提示下,我寫了關于如何從任意(位置)數字系統轉換為我們(或想要)進行計算的答案。(在該示例中,我從三進制轉換為十進制,而問題是從十進制轉換為二進制。)
在這里,我們想從任意數字系統(好的,基數在2到36之間,所以我們可以用來將一位數字Character.digit()轉換為整數)轉換為帶有基數BASE(= 1.000.000.000,但這在這里并不重要) 。
基本上,我們使用Horner方案來計算多項式的值,該數字在基數給定的點處作為系數。
sum[i=0..n] digit[i] * radix^i
可以使用以下循環計算:
value = 0;
for? i = n .. 0
value = value * radix + digit[i]
return value
由于我們的輸入字符串是big-endian,因此我們不必倒數,而是可以使用簡單的增強型for循環。(在Java中看起來更難看,因為我們沒有運算符重載,也沒有從int到DecimalBigInt類型的自動裝箱。)
public static DecimalBigInt valueOf(String text, int radix) {
DecimalBigInt bigRadix = new DecimalBigInt(radix);
DecimalBigInt value = new DecimalBigInt(); // 0
for(char digit : text.toCharArray()) {
DecimalBigInt bigDigit =
new DecimalBigInt(Character.digit(digit, radix));
value = value.times(bigRadix).plus(bigDigit);
}
return value;
}
在我的實際實現中,我添加了一些錯誤檢查(和引發異常),以確保我們確實有一個有效的數字,當然還有文檔注釋。
轉換為任意位置系統更為復雜,因為它涉及余數和除法(按任意基數),而我們尚未實現,因此目前還沒有實現。當我對分割方法有個好主意時,它將完成。(在這里我們只需要用小數(一位數字)進行除法,這可能比一般的除法更容易。)
小數除法
在學校里,我學會了長除法。這是一個小(一位數字)除數的示例,我們在德國使用的表示法(帶有關于背景計算的注釋,我們通常不會寫),以十進制表示:
12345 : 6 = 02057? ? ?1 / 6 =? 0
-0┊┊┊┊? ? ? ? ? ? ? ? ?0 * 6 =? 0
──┊┊┊┊
12┊┊┊? ? ? ? ? ? ? ? 12 / 6 =? 2
-12┊┊┊? ? ? ? ? ? ? ? ?2 * 6 = 12
──┊┊┊
03┊┊? ? ? ? ? ? ? ? ?3 / 6 =? 0
- 0┊┊? ? ? ? ? ? ? ? ?0 * 6 =? 0
──┊┊
34┊? ? ? ? ? ? ? ? 34 / 6 =? 5
-30┊? ? ? ? ? ? ? ? ?5 * 6 = 30
──┊
45? ? ? ? ? ? ? ? 45 / 6 =? 7
-42? ? ? ? ? ? ? ? ?7 * 6 = 42
──
3? ? ?==> quotient 2057, remainder 3.
當然,如果我們有本征余數運算,則不需要計算這些乘積(0、12、0、30、42)并減去它們。然后看起來像這樣(當然,我們這里不需要編寫操作):
12345 : 6 = 02057? ? ?1 / 6 =? 0,? ?1 % 6 = 1
12┊┊┊? ? ? ? ? ? ? ? 12 / 6 =? 2,? 12 % 6 = 0
03┊┊? ? ? ? ? ? ? ? ?3 / 6 =? 0,? ?3 % 6 = 3
34┊? ? ? ? ? ? ? ? 34 / 6 =? 5,? 34 % 6 = 4
45? ? ? ? ? ? ? ? 45 / 6 =? 7,? 45 % 6 = 3
3
==> quotient 2057, remainder 3.
如果我們用另一種格式編寫的話,這看起來已經很像短除法了。
我們可以觀察(并證明)以下內容:
如果我們有一個兩位數的數字x,其第一位數字小于我們的除數d,x / d則它是一位數,并且x % d也是一位數字,小于d。這與歸納一起表明,我們只需要用除數除以兩位數(余數)即可。
回到以BASE為基數的大數字:所有兩位數字都可以用Java表示long,那里有native /和%。
/**
* does one step in the short division algorithm, i.e. divides
*? a two-digit number by a one-digit one.
*
* @param result the array to put the quotient digit in.
* @param resultIndex the index in the result array where
*? ? ? ? ? ? ?the quotient digit should be put.
* @param divident the last digit of the divident.
* @param lastRemainder the first digit of the divident (being the
*? ? ? ? ? ?remainder of the operation one digit to the left).
*? ? ? ? ? ?This must be < divisor.
* @param divisor the divisor.
* @returns the remainder of the division operation.
*/
private int divideDigit(int[] result, int resultIndex,
int divident, int lastRemainder,
int divisor) {
assert divisor < BASE;
assert lastRemainder < divisor;
long ent = divident + (long)BASE * lastRemainder;
long quot = ent / divisor;
long rem = ent % divisor;
assert quot < BASE;
assert rem < divisor;
result[resultIndex] = (int)quot;
return (int)rem;
}
現在,我們將循環調用此方法,始終將前一次調用的結果反饋為lastRemainder。
/**
* The short division algorithm, like described in
* Wikipedia's
*? ?article Short division.
* @param result an array where we should put the quotient digits in.
* @param resultIndex the index in the array where the highest order digit
*? ? ?should be put, the next digits will follow.
* @param divident the array with the divident's digits. (These will only
*? ? ? ? ? be read, not written to.)
* @param dividentIndex the index in the divident array where we should
*? ? ? ? ?start dividing. We will continue until the end of the array.
* @param divisor the divisor. This must be a number smaller than
*? ? ? ? {@link #BASE}.
* @return the remainder, which will be a number smaller than
*? ? ?{@code divisor}.
*/
private int divideDigits(int[] result, int resultIndex,
int[] divident, int dividentIndex,
int divisor) {
int remainder = 0;
for(; dividentIndex < divident.length; dividentIndex++, resultIndex++) {
remainder = divideDigit(result, resultIndex,
divident[dividentIndex],
remainder, divisor);
}
return remainder;
}
此方法仍返回一個int,其余為。
現在我們要有一個返回DecimalBigInt的公共方法,所以我們創建一個。它的任務是檢查參數,為工作方法創建一個數組,丟棄其余部分并從結果中創建DecimalBigInt。(構造函數刪除可能存在的前導零。)
/**
* Divides this number by a small number.
* @param divisor an integer with {@code 0 < divisor < BASE}.
* @return the integer part of the quotient, ignoring the remainder.
* @throws IllegalArgumentException if the divisor is <= 0 or >= BASE.
*/
public DecimalBigInt divideBy(int divisor)
{
if(divisor <= 0 || BASE <= divisor) {
throw new IllegalArgumentException("divisor " + divisor +
" out of range!");
}
int[] result = new int[digits.length];
divideDigits(result, 0,
digits, 0,
divisor);
return new DecimalBigInt(result);
}
我們還有一個類似的方法,它返回余數:
/**
* Divides this number by a small number, returning the remainder.
* @param divisor an integer with {@code 0 < divisor < BASE}.
* @return the remainder from the division {@code this / divisor}.
* @throws IllegalArgumentException if the divisor is <= 0 or >= BASE.
*/
public int modulo(int divisor) {
if(divisor <= 0 || BASE <= divisor) {
throw new IllegalArgumentException("divisor " + divisor +
" out of range!");
}
int[] result = new int[digits.length];
return divideDigits(result, 0,
digits, 0,
divisor);
}
這些方法可以這樣調用:
DecimalBigInt d3_by_100 = d3.divideBy(100);
System.out.println("d3/100 = " + d3_by_100);
System.out.println("d3%100 = " + d3.modulo(100));
轉換為任意基數
現在我們有了轉換為任意基數的基礎。當然,不是真正任意的,只有基數小于BASE允許的基數,但這應該不是太大的問題。
正如在有關轉換數字的另一個答案中已經回答的那樣,我們必須執行“除法,余數,乘法,加法”。“乘加”部分實際上只是將各個數字放在一起,因此我們可以用一個簡單的數組代替它-訪問。
由于我們總是需要商和余數,因此我們將不使用public方法modulo和divideBy,而是反復調用該divideDigits方法。
/**
* converts this number to an arbitrary radix.
* @param radix the target radix, {@code 1 < radix < BASE}.
* @return the digits of this number in the base-radix system,
*? ? ?in big-endian order.
*/
public int[] convertTo(int radix)
{
if(radix <= 1 || BASE <= radix) {
throw new IllegalArgumentException("radix " + radix +
" out of range!");
}
首先,對0進行特殊處理。
// zero has no digits.
if(digits.length == 0)
return new int[0];
然后,我們為結果數字(足夠長)和一些其他變量創建一個數組。
// raw estimation how many output digits we will need.
// This is just enough in cases like BASE-1, and up to
// 30 digits (for base 2) too much for something like (1,0,0).
int len = (int) (Math.log(BASE) / Math.log(radix) * digits.length)+1;
int[] rDigits = new int[len];
int rIndex = len-1;
int[] current = digits;
int quotLen = digits.length;
quotLen是最后一個商的位數(不包括前導零)。如果為0,則完成。
while(quotLen > 0)? {
下一個商的新數組。
int[] quot = new int[quotLen];
商與余數運算。商現在在quot,余數在rem。
int rem = divideDigits(quot, 0,
current, current.length - quotLen,
radix);
我們將其余部分放在輸出數組中(從最后一位開始填充)。
rDigits[rIndex] = rem;
rIndex --;
然后我們將數組交換到下一輪。
current = quot;
如果商中有前導零(由于基數小于BASE,最大為1),我們將商大小縮小1。下一個數組將更小。
if(current[0] == 0) {
// omit leading zeros in next round.
quotLen--;
}
}
循環之后,rDigits數組中可能有前導零,我們將其切除。
// cut of leading zeros in rDigits:
while(rIndex < 0 || rDigits[rIndex] == 0) {
rIndex++;
}
return Arrays.copyOfRange(rDigits, rIndex, rDigits.length);
}
而已。但是,它看起來有點復雜。這是一個如何使用它的示例:
System.out.println("d4 in base 11: " +
Arrays.toString(d4.convertTo(11)));
System.out.println("d5 in base 7: " +
Arrays.toString(d5.convertTo(7)));
它們打印[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]和[1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0],與我們之前解析的數字相同(不過是從字符串中提取的)。
基于此,我們還可以將其格式化為字符串:
/**
* Converts the number to a String in a given radix.
* This uses {@link Character.digit} to convert each digit
* to one character.
* @param radix the radix to use, between {@link Character.MIN_RADIX}
*? ?and {@link Character.MAX_RADIX}.
* @return a String containing the digits of this number in the
*? ?specified radix, using '0' .. '9' and 'a' .. 'z' (as much as needed).
*/
public String toString(int radix) {
if(radix < Character.MIN_RADIX || Character.MAX_RADIX < radix) {
throw new IllegalArgumentException("radix out of range: " + radix);
}
if(digits.length == 0)
return "0";
int[] rdigits = convertTo(radix);
StringBuilder b = new StringBuilder(rdigits.length);
for(int dig : rdigits) {
b.append(Character.forDigit(dig, radix));
}
return b.toString();
}
總結
以上是生活随笔為你收集整理的java biginteger转int_如何在不使用java.math.BigInteger的情况下使用Java处理非常大的数字...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 初始化一个实体对象_Java
- 下一篇: maven java archetype