如何在 Java 中处理非常大的数字而不使用 java.math.BigInteger
在不使用 java.math.BigInteger 的情况下,如何使用任意大的整数进行算术 + - / * % !?
例如,在 Java 中,90 的阶乘返回 0。 我希望能够解决这个问题。
How would I go about doing arithmetic, + - / * % !, with arbitrarily large integers without using java.math.BigInteger
?
For instance, the factorial of 90 returns 0 in Java.
I would like to be able to solve that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为程序员应该实现过一次自己的bignum-library,所以欢迎来到这里。
(当然,稍后你会发现BigInteger更好,并使用它,但这是一个宝贵的学习经验。)
(你可以关注本课程life的源代码 在 github 上。此外,我将其重新制作(有点抛光)为14 部分博客系列.)
在 Java 中创建一个简单的 Big number 类
那么,我们需要什么?
的数字表示
首先,基于 Java 提供的数据类型
。当您认为十进制转换是最复杂的部分时,让我们保持基于十进制的模式。为了提高效率,我们不会存储真正的十进制数字,而是以基数
1 000 000 000 = 10^9
1 000 000 000 = 10^9
2^30
。这适合 Javaint
(最多2^31
或2^32
),以及两个这样的数字的乘积< /em> 非常适合 Javalong
。然后是数字数组:
我们是以小端还是大端存储数字,即较大的部分在前还是在最后?这并不重要,所以我们决定使用大端字节序,因为这就是人类想要阅读的方式。 (现在我们专注于非负值 - 稍后我们将为负数添加一个符号位。)
出于测试目的,我们添加一个允许从这样的 int[] 进行初始化的构造函数。
作为一个额外的好处,这个构造函数还可以用于单个
int
(如果小于BASE
),甚至可以用于没有int
(我们将解释为 0)。所以,我们现在可以这样做:这给了我们
de.fencing_game.paul.examples.DecimalBigInt@6af62373
,但不太有用。所以,我们添加一个toString()
方法:现在的输出是
Big[7, 5, 2, 12345]
,这对于测试来说更有用,不是吗? ?二、十进制格式的转换。
我们很幸运:我们的基数 (10^9) 是我们想要从 (10) 转换的基数的幂。因此,我们总是有相同数量 (9) 的十进制数字代表一个“我们的格式”数字。 (当然,一开始可能会少一些数字。)在下面的代码中,
decimal
是一个十进制数字的字符串。这个奇怪的公式是 Java int 的写法:bigLen = ceil(decLen/BASE_DECIMAL_DIGITS)。 (我希望它是正确的,我们稍后会测试它。)
这是第一个十进制数字块的长度,应该在 1 到 9 之间(包括 1 和 9)。
我们创建数组:
循环遍历要创建的数字:
每个数字都由原始数字中的数字块表示:(
需要
Math.max
这里是第一个较短的块。)我们现在使用常用的整数解析函数,并将结果放入数组中:
从现在创建的数组中,我们创建了 DecimalBigInt 对象:
让我们看看这是否有效:
输出:
看起来正确:-) 我们应该用一些其他数字来测试它(不同长度)也。
下一部分将是十进制格式,这应该更容易。
三、转换为十进制格式。
我们需要将每个数字输出为 9 位十进制数字。为此,我们可以使用 Formatter 类,它支持类似 printf 的格式字符串。
一个简单的变体是这样的:
这对于我们的两个数字返回
000000007000000005000000002000012345
和000000012345678901234567890
。这适用于往返(即,将其提供给valueOf
方法,给出一个等效的对象),但前导零看起来不太好(并且可能会与八进制数产生混淆)。因此,我们需要分解漂亮的 for-each 循环,并对第一个和后面的数字使用不同的格式字符串。添加。
让我们从加法开始,因为这很简单(稍后我们可以将其部分用于乘法)。
我希望您可以像阅读公式一样阅读方法名称,因此使用
plus
、minus
、times
而不是add、<代码>减法、<代码>乘法。
那么,加法是如何工作的呢?它的工作原理与我们在学校学到的大于 9 的十进制数相同:将相应的数字相加,如果其中某些数字的结果大于 10(或在我们的例子中为
BASE
),则进位一到下一个数字。这可能会导致结果数字比原始数字多一位数字。首先我们看一下两个数字具有相同位数的简单情况。那么它看起来就像这样:(
我们从右到左,所以我们可以将任何溢出带到下一个数字。如果我们决定使用 Little Endian 格式,这会更漂亮。)
如果两个数字不具有相同的值位数,它变得有点复杂。
为了让它尽可能简单,我们将其分成几个方法:
此方法将一位数字添加到数组中的元素(可能已经包含一些非零值),并将结果存储回数组中。如果存在溢出,我们通过递归调用将其移至下一位(其索引少一位,而不是多一位)。这样我们就可以确保我们的数字始终保持在有效范围内。
接下来对要添加的整个数字数组执行相同的操作:
现在我们可以实现我们的
plus
方法:如果我们先看看是否有可能发生溢出,并且只有在那时,我们可以做得更好一点创建比需要大一的数组。
啊,一个测试:
d2.plus(d2)
给出了Big[24, 691357802, 469135780]
,看起来是正确的。乘法。
让我们回想一下,回到学校,我们如何在纸上乘以更大的数字?
因此,我们必须将第一个数字的每个数字[i]与第二个数字的每个数字[j]相乘,并将结果添加到结果的数字[i+j]中(注意进位)。当然,这里的索引是从右开始计算的,而不是从左开始。 (现在我真的希望我使用的是小端数字。)
由于两个数字的乘积可能超出
int
的范围,因此我们使用long 用于乘法。
现在我们可以明白为什么我声明我的
addDigits
方法来接受resultIndex
参数。 (我刚刚将最后一个参数更改为 varargs 参数,以便能够在这里更好地编写它。)因此,这里是交叉相乘方法:
我希望我的索引计算正确。使用小端表示法,它会是
multiplyDigit(result, resultIndex + i + j, leftFactor[i], rightFactor[j])
- 更清楚,不是吗?我们的 times 方法现在只需分配结果数组,调用 multiplyDigits 并包装结果。
为了测试,
d2.times(d2)
给出Big[152, 415787532, 388367501, 905199875, 19052100]
,这与我的 Emacs calc 在这里计算的结果相同。比较
我们希望能够比较两个对象。因此,我们实现了Comparable及其compareTo方法。
如何知道我们的一个数字是否大于另一个数字?首先,我们比较数组的长度。由于我们注意不要引入任何前导零(是吗?),较长的数组应该具有较大的数字。
如果长度相同,我们可以按元素进行比较。由于我们使用大端(即大端先行),所以我们从头开始。
如果一切都相同,显然我们的数字是相同的,我们可以返回
0
。equals
+hashCode()
每个好的不可变类都应该以合适的方式实现
equals()
和hashCode()
和兼容)方式。对于我们的
hashCode()
,我们只需将数字相加,将它们与一个小质数相乘,以确保数字切换不会产生相同的哈希码:在
equals() 方法我们只需委托给compareTo 方法,而不是再次实现相同的算法:
所以,今天就足够了。减法(可能还有负数)和除法更复杂,所以我现在省略它们。 对于计算 90 的阶乘,这应该足够了。
计算大阶乘:
这里是阶乘函数:
这为我们提供了
从任意基数表示形式的转换
在 frodosamoa 的下一个问题的提示下,我写了 我关于如何从任意转换的答案我们可以(或想要)计算的(位置)数字系统。 (在这个例子中,我从三进制转换为十进制,而问题是关于十进制到二进制。)
这里我们想要从任意数字系统转换(好的,基数在 2 到 36 之间,所以我们可以使用
Character.digit( )
将个位数转换为整数)到我们的系统,基数为BASE
(= 1.000.000.000,但这在这里并不重要)。基本上,我们使用 Horner 方案 来计算多项式的值,其中数字为系数,给出的点为基数。
可以用这个循环计算:
由于我们的输入字符串是大端字节序,所以我们不必倒数,但可以使用一个简单的增强型 for 循环。
(在 Java 中看起来更难看,因为我们没有运算符重载,也没有从 int 到我们的自动装箱
DecimalBigInt 类型。)
在我的实际实现中,我添加了一些错误检查(和异常抛出) )以确保我们确实有一个有效的数字,当然还有文档注释。
转换到任意位置系统更为复杂,因为它涉及余数和除法(按任意基数),而我们尚未实现 - 所以现在还没有实现。当我对如何进行除法有了好主意时就会完成。 (这里我们只需要除以小数(一位数),这可能比一般除法更容易。)
小数除法
在学校,我学到了 长除法。这是一个小(一位数)除数的示例,采用我们在德国使用的符号(带有有关背景计算的注释,我们通常不会编写),采用十进制:
当然,我们不需要计算这些乘积 (0, 12, 0, 30, 42)
如果我们有本机余数运算,则减去它们。然后看起来
像这样(当然,我们这里不需要编写操作):
这看起来已经很像 短除法< /a>,如果我们用另一种格式来写。
我们可以观察(并证明)以下内容:
如果我们有一个两位数 x,其第一位数字小于除数 d,则
x / d
是一位数,并且x % d
也是一位数,小于 d。这与归纳一起表明我们只需要用除数除(带余数)两位数。回到基数 BASE 的大数字:所有两位数都可以表示为 Java
long
,并且我们有本机/
和%
>。现在,我们将在循环中调用此方法,始终将上一次回调的结果作为
lastRemainder
提供。这个方法仍然返回一个int,即余数。
现在我们想要一个返回 DecimalBigInt 的公共方法,因此我们创建一个。它的任务是检查参数、为工作方法创建一个数组、丢弃剩余部分,并根据结果创建一个 DecimalBigInt。 (构造函数删除了可能存在的前导零。)
我们还有一个类似的方法,它返回余数:
这些方法可以像这样调用:
转换为任意基数
现在我们已经掌握了转换为任意基数的基础知识。当然,这并不是任意的,只允许小于
BASE
的基数,但这不应该是一个太大的问题。正如在另一个关于转换数字的答案中已经回答的那样,我们必须进行“除法,余数,乘法,加法”。“乘加”部分实际上只是将各个数字放在一起,所以我们可以用一个简单的数组替换它 -
由于我们总是需要商和余数,因此我们不会使用公共方法
modulo
和divideBy
,而是重复调用divideDigits code> 方法。
首先,对 0 进行特殊情况处理。
然后,我们为结果数字创建一个数组(足够长),
以及其他一些变量。
quotLen
是最后一个商中的位数(不包括前导零)。如果这是 0,我们就完成了。下一个商的新数组。
商和余数运算。商现在位于
quot
中,余数在
rem
中。我们将余数放入输出数组中(从最后一位数字开始填充)。
然后我们交换下一轮的数组。
如果商中有前导零(最多有一个,因为
基数小于 BASE),我们将商大小缩小一。下一个数组
会更小。
循环结束后,rDigits 数组中可能会有前导零,我们将它们剪掉。
就是这样。不过看起来有点复杂。以下是如何使用它的示例:
这些 print
[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]
,与我们之前解析的数字相同(不过来自字符串)。基于此我们还可以格式化为字符串:
I think a programmer should have implemented his own bignum-library once, so welcome here.
(Of course, later you'll get that BigInteger is better, and use this, but it is a valuable learning experience.)
(You can follow the source code of this course life on github. Also, I remade this (a bit polished) into a 14-part blog series.)
Creating a simple Big number class in Java
So, what do we need?
First, a representation of the number,
based on the datatypes which Java gives us.
As you think the decimal conversion is the most complicated part, let's stay in a decimal based mode. For efficiency, we'll store not real decimal digits, but work in base
1 000 000 000 = 10^9 < 2^30
. This fits in a Javaint
(up to2^31
or2^32
), and the product of two such digits fits nicely in a Javalong
.Then the digits-array:
Do we store the digits in little- or big endian, i.e. the bigger parts first or last? It does not really matter, so we decide on big-endian since this is how humans want to read it. (For now we concentrate on non-negative values - later we'll add a sign bit for negative numbers.)
For testing purposes, we add a constructor which allows initializing from such a int[].
As a added bonus, this constructor is also usable for a single
int
(if smaller thanBASE
), and even for noint
(which we'll interpret as 0). So, we now can do this:This gives us
de.fencing_game.paul.examples.DecimalBigInt@6af62373
, not so useful. So, we add atoString()
method:The output is now
Big[7, 5, 2, 12345]
, which is more useful for testing, isn't it?Second, conversion from decimal format.
We are lucky here: our base (10^9) is a power of the base we want to convert from (10). Thus, we always have the same number (9) of decimal digits representing one "our format" digit. (Of course, in the beginning there may be some digits less.) In the following code,
decimal
is a String of decimal digits.This strange formula is a Java int way of writing
bigLen = ceil(decLen/BASE_DECIMAL_DIGITS)
. (I hope it is correct, we'll later test it.)This is the length of the first block of decimal digits, should be between 1 and 9 (inclusive).
We create our array:
Looping through the digits to be created:
Each of our digits is represented by a block of digits in the original number:
(The
Math.max
is needed here for the first shorter block.)We now use the usual Integer parsing function, and put the result into the array:
From the array now created we create our DecimalBigInt object:
Let's see if this works:
Output:
Looks right :-) We should test it with some other numbers (of different length) too.
Next part will be decimal formatting, this should be even easier.
Third, conversion to decimal format.
We need to output our individual digits as 9 decimal digits each. For this we can use the
Formatter
class, which supports printf-like format strings.A simple variant would be this:
This returns
000000007000000005000000002000012345
and000000012345678901234567890
for our two numbers. This works for a round-trip (i.e. feeding it to thevalueOf
method gives an equivalent object), but the leading zeros are not really nice to look at (and could create confusion with octal numbers). So we need to break apart our beautiful for-each loop and use a different formatting string for the first and the following digits.Addition.
Let's start with addition, as this is simple (and we can use parts of it for the multiplication later).
I want method names that you can read like you would read the formula, thus
plus
,minus
,times
instead ofadd
,subtract
,multiply
.So, how does addition work? It works the same as we learned it in school for decimal numbers higher than 9: add the corresponding digits, and if for some of then the result is bigger than 10 (or
BASE
in our case), carry one to the next digit. This can cause the resulting number to have one digit more than the original ones.First we look at the simple case that both numbers have same number of digits. Then it looks simply like this:
(We go from right to left, so we can carry any overflows to the next digit. This would be a bit prettier if we had decided using Little Endian format.)
If both numbers do not have the same number of digits, it gets a bit more complicated.
To let it as simple as possible, we split it to several methods:
This method adds one digit to an element in the array (which may already contain some non-zero value), and stores the result back in the array. If there was overflow, we carry it to the next digit (which has index one less, not one more) by means of a recursive call. This way we make sure our digits stay always in the valid range.
The next does the same for a whole array of digits to add:
Now we can implement our
plus
method:We could do a bit better here if we would look before if overflow is at all possible and only then create the array one bigger than necessary.
Ah, one test:
d2.plus(d2)
givesBig[24, 691357802, 469135780]
, which looks right.Multiplication.
Let's remember back to school, how did we multiply bigger numbers on paper?
So, we have to multiply each digit[i] of the first number with each digit[j] of the second number, and add the product in digit[i+j] of the result (and pay attention to carry). Of course, here the indexes are counted from right, not from left. (Now i really wish I had used little-endian numbers.)
Since the product of two of our digits can get outside of the range of
int
, we uselong
for multiplication.Now we can see why I declared my
addDigits
method to take aresultIndex
parameter. (And I just changed the last argument to a varargs parameter, to be able to write this here better.)So, here the cross-multiplying method:
I hope I have the index-calculations right. With a little-endian representation, it would have been
multiplyDigit(result, resultIndex + i + j, leftFactor[i], rightFactor[j])
- quite clearer, isn't it?Our
times
method now has only to allocate the result array, invokemultiplyDigits
and wrap the result.For testing,
d2.times(d2)
givesBig[152, 415787532, 388367501, 905199875, 19052100]
, which is the same what my Emacs calc calculates here.Comparison
We want to be able to compare two of our objects. So, we implement
Comparable<DecimalBigInt>
and its compareTo method.How to know if one of our numbers is bigger than another? First, we compare the length of the arrays. As we took care not to induce any leading zeros (did we?), the longer array should have the bigger number.
If the length are same, we can compare elementwise. Since we use big endian (i.e. the big end comes first), we start at the beginning.
If everything was same, obviously our numbers are identical, and we can return
0
.equals
+hashCode()
Every good immutable class should implement
equals()
andhashCode()
in a suitable (and compatible) way.For our
hashCode()
, we simply sum up the digits, multiplying them with a small prime to make sure digit-switching does not result in same hash code:In the
equals()
method we simply can delegate to the compareTo method, instead of implementing the same algorithm again:So, enough for today. Subtraction (and maybe negative numbers) and division are more complicated, so I'm omitting them for now. For calculating the factorial of 90 this should be enough.
Calculating big factorials:
Here the factorial function:
This gives us
Converting from arbitrary-radix representations
Prompted by the next question of frodosamoa, I wrote my answer about how to convert from arbitrary (positional) number systems in the one in which we can (or want to) calculate. (In the example there, I converted from trinary to decimal, while the question was about decimal to binary.)
Here we want to convert from an arbitrary number system (okay, with radix between 2 and 36, so we can use
Character.digit()
to convert single digits to ints) to our system with radixBASE
(= 1.000.000.000, but this is not really important here).Basically we use Horner scheme to calculate the value of polynomial with the digits as coefficients at the point given by the radix.
can be calculated with this loop:
Since our input strings are big-endian, we don't have to count down, but can use a simple enhanced for loop.
(It looks more ugly in Java, since we have no operator overloading, and no autoboxing from int to our
DecimalBigInt type.)
In my actual implementation I added some error checking (and exception throwing) to ensure that we really have a valid number, and of course a documentation comment.
Converting to an arbitrary positional system is more complicated, as it involves remainder and division (by the arbitrary radix), which we did not implement yet - so not for now. It will be done when I have a good idea on how to do division. (We need only division by small (one-digit) numbers here, which may be easier than a general division.)
Division by small numbers
In school, I learned long division. Here is an example for a small (one-digit) divisor, in the notation we use here in Germany (with annotations about the background calculations, which we normally would not write), in decimal system:
Of couse, we don't need to calculate these products (0, 12, 0, 30, 42)
and subtract them if we have a native remainder operation. Then it looks
like this (of course, we here would not need to write the operations):
This already looks quite like short division, if we write it in another format.
We can observe (and prove) the following:
If we have a two-digit number x with first digit smaller than our divisor d, than
x / d
is a one-digit number, andx % d
is also a one-digit number, smaller than d. This, together with induction, shows that we only ever need to divide (with remainder) two-digit numbers by our divisor.Coming back to our big numbers with radix BASE: all two-digit numbers are representable as a Java
long
, and there we have native/
and%
.We will now call this method in a loop, always feeding the result from the previous call back as
lastRemainder
.This method still returns an int, the remainder.
Now we want to have a public method returning a DecimalBigInt, so we create one. It has the task to check the arguments, create an array for the working method, discard the remainder, and create a DecimalBigInt from the result. (The constructor removes a leading zero which may be there.)
We also have a similar method, which returns the remainder instead:
These methods can be invoked like this:
Conversion to arbitrary radix
Now we have the basics to convert to an arbitrary radix. Of course, not really arbitrary, only radixes smaller than
BASE
are allowed, but this should not be a too big problem.As already answered in another answer about converting numbers, we have to do "division, remainder, multiply, add. The "multiply-add" part is in fact only putting together the individual digits, so we can replace it by a simple array-access.
As we always need both the quotient and the remainder, we won't use the public methods
modulo
anddivideBy
, but instead repeatedly call thedivideDigits
method.First, a special-case handling for 0.
Then, we create an array for the result digits (long enough),
and some other variables.
quotLen
is the number of digits (excluding leading zeroes) in the last quotient. If this is 0, we are done.A new array for the next quotient.
The quotient-and-remainder operation. The quotient is now in
quot
,the remainder in
rem
.We put the remainder in the output array (filling it from the last digit).
Then we swap the arrays for the next round.
If there are leading zeros in the quotient (there will be at most one, since
radix is smaller than BASE), we shrink the quotient size by one. The next array
will be smaller.
After the loop there may be leading zeros in the rDigits array, and we cut them off.
That's it. It looks a bit complicated, though. Here is an example of how to use it:
These print
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
and[1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0]
, just the same numbers as we parsed before (from a String, though).Based on this we can also format as a string:
如果您尝试的话,您可能想实现或研究二进制编码的十进制的库以避免 BigInteger。如果您想使用 BigInteger,您可以使用它来完成 90 的阶乘:
You might want to implement or research a library for binary-coded decimal if you're trying to avoid
BigInteger
. You can accomplish factorial of 90 withBigInteger
if you want to use it though:使用下面的代码来乘以任意长度的数字:-
Use the code below to multiply numbers of any length:-
Java 中使用运算符
+
、-
、*
、/
和% 受 Java 原始数据类型 的约束。
这意味着,如果您无法将所需的数字放入
double
或long
的范围内,那么您将不得不使用“大数字”库,例如 Java 内置的 (BigDecimal ,BigInteger) ,或第三方库,或自己编写。这也意味着您不能使用算术运算符,因为 Java 不支持运算符重载。Arithmetic operations in Java using the operators
+
,-
,*
,/
, and%
are bound by the constraints of the Java primitive data types.This means that if you can't fit your desired numbers into the range of, say a
double
orlong
then you'll have to use a "big number" library, such as the one built-in to Java (BigDecimal, BigInteger), or a third-party library, or write your own. This also means that you cannot use the arithmetic operators since Java does not support operator overloading.当我想做90的时候!或其他一些大规模计算,我尝试使用 int[] 数组,每个元素保存一个数字。然后我应用传统的乘法,使用笔和纸在另一个 int[] 数组中得到答案。
这是我用 Java 编写的计算 100 的代码!相当快。您可以随意使用它。
When I want to do 90! or some other massive calculation, I try and use an int[] array, each element holding one of the digits. Then I apply the traditional multiplication we using pen and paper to get the answer in another int[] array.
This is the code I wrote in Java which calculates 100! rather quickly. Feel free to use this however you like.
强文本公共类 BigInteger {
}
strong text public class BigInteger {
}
如果我们想要对非常大的数字执行算术运算,那么它们必须采用某种对象形式,例如字符串。
令它们为字符长度大于BigInteger范围的字符串。
在本例中,我将像在笔记本上执行算术运算一样执行算术运算。
例如 - 假设我们必须进行加法。
首先比较两个字符串的长度。
制作三个新弦。
第一个字符串是较小的字符串。
第二个字符串是较长字符串的最右边子字符串,其长度等于较小字符串。
第三根弦是左边剩下的长弦。
现在添加末尾的第一个和第二个字符串,将字符转换为整数,一次一个字符,并将进位保留在 int 变量中。每次相加后,立即将总和追加到 StringBuffer 中。两个字符串相加后,对第三个字符串进行同样的操作,继续添加进位。最后反转StringBuffer并返回String。
这是我用于加法的代码
If we have really big numbers on which we want to perform arithmetic operations than they must be in some object form such as Strings.
Let their be strings with the character length greater than the range of BigInteger.
In this case I'll perform arithmetic operation the way we do it on a notebook.
For Example - Let's assume we have to do the addition.
Start with comparing the two strings for length.
Make three new Strings.
The First String is the smaller one.
The Second String is the rightmost substring of the longer string with length equal to the smaller string.
The third string is the leftover long string from the left side.
Now add the first and second string from the end converting characters to integers, one character at a time and keeping the carry in an int variable. Immediately after each addition, append the sum in a StringBuffer. After the two strings are added, do the same operation for the third string and keep on adding the carry. In the end reverse the StringBuffer and return the String.
Here is the code I used for Addition