Using BigInteger Multiply operator

大兔子大兔子 提交于 2019-12-29 04:21:06

问题


I was wondering if there was a way to multiply BigInteger variables together, because the * operator cannot be applied to BigInteger.

So I was wondering if it was possible to multiply two BigIntegers together without using the * operator.


回答1:


You use BigIntegers multiply() method like so:

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.




回答2:


Easier way to implement:

int i = 5;
BigInteger bigInt = new BigInteger("12345678901");
BigInteger result = bigInt.multiply(BigInteger.valueOf(i))



回答3:


You can use the multiply(BigInteger) method in BigInteger. So:

BigInteger result = someBigInt.multiply(anotherBigInt);

BigInteger in Java API




回答4:


The result multiplying these specific factors

A: 131224324234234234234313

B: 13345663456346435648234313

Could be this one (I hope I am correct):

R: 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;



来源:https://stackoverflow.com/questions/3877765/using-biginteger-multiply-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!