Java 使用 BigInteger 乘法运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3877765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using BigInteger Multiply operator
提问by Steffan Harris
I was wondering if there was a way to multiply BigInteger
variables together, because the *
operator cannot be applied to BigInteger
.
我想知道是否有办法将BigInteger
变量相乘,因为*
运算符不能应用于BigInteger
.
So I was wondering if it was possible to multiply two BigIntegers
together without using the *
operator.
所以我想知道是否可以在BigIntegers
不使用*
运算符的情况下将两个相乘。
采纳答案by jjnguy
You use BigInteger
smultiply()
method like so:
您可以像这样使用BigInteger
smultiply()
方法:
BigInteger int1 = new BigInteger("131224324234234234234313");
BigInteger int2 = new BigInteger("13345663456346435648234313");
BigInteger result = int1.multiply(int2)
I should have pointed out a while ago that BigInteger
is immutable. So any result of an operation has to be stored into a variable. The operator or operand are never changed.
我应该在不久前指出这BigInteger
是不可变的。因此,操作的任何结果都必须存储到变量中。运算符或操作数永远不会改变。
回答by swilliams
You can use the multiply(BigInteger) method in BigInteger. So:
您可以在 BigInteger 中使用 multiply(BigInteger) 方法。所以:
BigInteger result = someBigInt.multiply(anotherBigInt);
回答by waweru
Easier way to implement:
更简单的实现方式:
int i = 5;
BigInteger bigInt = new BigInteger("12345678901");
BigInteger result = bigInt.multiply(BigInteger.valueOf(i))
回答by wolfran
The result multiplying these specific factors
乘以这些特定因素的结果
A: 131224324234234234234313
答:131224324234234234234313
B: 13345663456346435648234313
乙:13345663456346435648234313
Could be this one (I hope I am correct):
可能是这个(我希望我是正确的):
R: 1751275668516575787795211751170772134115968581969
回复:1751275668516575787795211751170772134115968581969
Both are considered being two positive integers. And the technique used was Karatsuba's method
两者都被认为是两个正整数。使用的技术是唐叶的方法
int ab = (mul1) * 10^n + (mul3 - mul1 - mul2) * 10^n/2 + mul2;
int ab = (mul1) * 10^n + (mul3 - mul1 - mul2) * 10^n/2 + mul2;