BigInteger
Making a BigInteger
Object
There are several ways. There are constructors and a
static factory method.
What's the best way to get 1, 2, 0, and 10 as BigIntegers
?
jshell> BigInteger.ZERO
$1 ==> 0
jshell> BigInteger.ONE
$2 ==> 1
jshell> BigInteger.TWO
$3 ==> 2
jshell> BigInteger.TEN
$4 ==> 10
How do you convert the string "2355342324234" to a BigInteger
?
jshell> new BigInteger("2355342324234")
$5 ==> 2355342324234
Compute 2 + 4
jshell> a.add(b)
$8 ==> 6
jshell> a
a ==> 2
jshell> b.add(a)
$10 ==> 6
Compute 100 - 56
jshell> a.subtract(b)
$15 ==> 56
jshell> a.add(b.negate())
$16 ==> 56
Compute 99 * 45
jshell> a = BigInteger.valueOf(99)
a ==> 99
jshell> b = BigInteger.valueOf(45)
b ==> 45
jshell> a.times(b)
| Error:
| cannot find symbol
| symbol: method times(java.math.BigInteger)
| a.times(b)
| ^-----^
jshell> a.multiply(b)
$19 ==> 4455
Compute 12 / 4
jshell> a = BigInteger.valueOf(12);
a ==> 12
jshell> b = BigInteger.valueOf(4);
b ==> 4
jshell> a.divide(b)
$22 ==> 3
Compute 365 % 7
jshell> BigInteger a = BigInteger.valueOf(365);
a ==> 365
jshell> BigInteger b = BigInteger.valueOf(7)
b ==> 7
jshell> a.mod(b)
$25 ==> 1
Compute 21000
jshell> BigInteger base = BigInteger.TWO;
base ==> 2
jshell> base.pow(1000)
$27 ==> 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
Compute gcd(233232421323125, 33253413232123875)
jshell> a.gcd(b)
$30 ==> 6125
jshell> b.gcd(a)
$31 ==> 6125
Compute 1000!
jshell> BigInteger p = BigInteger.ONE;
p ==> 1
jshell> for(int k = 1; k <= 1000; k++)
...> {
...> p = p.multiply(BigInteger.valueOf(k));
...> }
jshell> p
p ==> 40238726007709377354370243392300398571937486421071463254379991042993851239862902059204420848696940480047998861019719605863166687299480855890132382966994459099742450408707375991882362772718873251977950595099527612087497546249704360141827809464649629105639388743788648733711918104582578364784997701247663288983595573543251318532395846307555740911426241747434934755342864657661166779739666882029120737914385371958824980812686783837455973174613608537953452422158659320192809087829730843139284440328123155861103697680135730421616874760967587134831202547858932076716913244842623613141250878020800026168315102734182797770478463586817016436502415369139828126481021309276 ... 096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
What is the double
value of 5100 that
you can extract from a BigInteger
jshell> BigInteger x = BigInteger.valueOf(5).pow(1000)
x ==> 9332636185032188789900895447238171696170914463717 ... 74558605253696441650390625
jshell> double d = BigInteger.valueOf(5).pow(1000).doubleValue()
d ==> Infinity
jshell> d
d ==> Infinity
jshell> double d = BigInteger.valueOf(5).pow(100).doubleValue()
d ==> 7.888609052210118E69
How do you convert 0b111010101011101010111101101001011010
to a
BigInteger
?/p>
How do you convert 0xFFCC22334222FF
to a
BigInteger
?
How do I get -5 from 5 as a BigInteger?
How do we represent 10100 in various bases