Block B

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?

Not this way.


hell> new BigInteger(1)
|  Error:
|  BigInteger(long) has private access in java.math.BigInteger
|  new BigInteger(1)
|  ^---------------^

This works.



jshell> BigInteger.valueOf(1)
$3 ==> 1

jshell> byte b = 3;
b ==> 3

jshell> BigInteger.valueOf(b)
$5 ==> 3

jshell> BigInteger.TWO
$6 ==> 2

jshell> BigInteger.TEN
$7 ==> 10

jshell> BigInteger.ZERO
$8 ==> 0

jshell> 

How do you convert the string "2355342324234" to a BigInteger?


jshell> new BigInteger("2355342324234")
$9 ==> 2355342324234

Compute 2 + 4


jshell> BigInteger a = BigInteger.TWO;
a ==> 2

jshell> BigInteger b = BigInteger.valueOf(4);
b ==> 4

jshell> a + b
|  Error:
|  bad operand types for binary operator '+'
|    first type:  java.math.BigInteger
|    second type: java.math.BigInteger
|  a + b
|  ^---^

jshell> a.add(b)
$12 ==> 6

jshell> b.add(a)
$13 ==> 6

Compute 100 - 56


jshell> a = BigInteger.valueOf(100);
a ==> 100

jshell> b = BigInteger.valueOf(56);
b ==> 56

jshell> a.subtract(b)
$16 ==> 44

Compute 99 * 45


jshell> a = BigInteger.valueOf(45);
a ==> 45

jshell> b = BigInteger.valueOf(99);
b ==> 99

jshell> a.multiply(b)
$19 ==> 4455

Compute 12 / 4


jshell> a = BigInteger.valueOf(12);
a ==> 12

jshell> b = BigInteger.valueOf(3);
b ==> 3

jshell> a.divide(b)
$22 ==> 4

Compute 365 % 7


jshell> a = BigInteger.valueOf(365);
a ==> 365

jshell> b = BigInteger.valueOf(7);
b ==> 7

jshell> a.mod(b)
$25 ==> 1

Compute 21000


jshell> BigInteger.TWO.pow(1000)
$26 ==>
10715086071862673209484250490600018105614048117055336074437503883703510511249361
22493198378815695858127594672917553146825187145285692314043598457757469857480393
45677748242309854210746050623711418779541821530464749835819412673987675591655439
46077062914571196477686542167660429831652624386837205668069376

Compute gcd(233232421323125, 33253413232123875)


jshell> a = new BigInteger("233232421323125");
a ==> 233232421323125

jshell> b = new BigInteger("33253413232123875");
b ==> 33253413232123875

jshell> a.gcd(b)
$29 ==> 6125

jshell> b.gcd(a)
$30 ==> 6125

Compute 1000!


jshell> BigInteger out  = BigInteger.ONE;
out ==> 1

jshell> for(int k = 1; k <= 1000; k++){out =
out.multiply(BigInteger.valueOf(k);}
|  Error:
|  ')' expected
|  for(int k = 1; k <= 1000; k++){out =
out.multiply(BigInteger.valueOf(k);}
|
^

jshell> for(int k = 1; k <= 1000; k++){out =
out.multiply(BigInteger.valueOf(k));}

jshell> out
out ==>
40238726007709377354370243392300398571937486421071463254379991042993851239862902
05920442084869694048004799886101971960586316668729948085589013238296699445909974
24504087073759918823627727188732519779505950995276120874975462497043601418278094
64649629105639388743788648733711918104582578364784997701247663288983595573543251
31853239584630755574091142624174743493475534286465766116677973966688202912073791
43853719588249808126867838374559731746136085379534524221586593201928090878297308
43139284440328123155861103697680135730421616874760967587134831202547858932076716
91324484262361314125087802080002616831510273418279777047846358681701643650241536
9139828126481021309276
...
09699637252423056085590370062427124341690900415369010593398383577793941097002775
34720000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000

What is the double value of 5100 that you can extract from a BigInteger


jshell> a = BigInteger.valueOf(5).pow(100);
a ==>
7888609052210118054117285652827862296732064351090230047702789306640625

jshell> a.doubleValue()
$35 ==> 7.888609052210118E69

jshell> a.intValue()
$36 ==> -1971132175

How do you convert 0b111010101011101010111101101001011010 to a BigInteger?


jshell> a = new BigInteger("111010101011101010111101101001011010", 2)
a ==> 63009708634

How do you convert 0xFFCC22334222FF to a BigInteger?


jshell> a = new BigInteger("FFCC22334222FF", 16);
a ==> 72000566322143999

How do I get -5 from 5 as a BigInteger?


jshell> a = BigInteger.valueOf(5);
a ==> 5

jshell> a.negate()
$41 ==> -5

How do we represent 10100 in various bases


jshell> a = BigInteger.TEN.pow(100)
a ==> 1000000000000000000000000000000000000000000000000 ...
00000000000000000000000000

jshell> a.toString(16);
$44 ==>
"1249ad2594c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f10000000000000000000000000"

jshell> a.toString(2)
$45 ==>
"100100100100110101101001001011001010011000011011111001110101100001011001001111
0000100110001001100111000001011111100111000101011001110010000001000111000100001
0001101001111100101010101011001001000011000010001010100000101110100011110001000
0000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000"

jshell> a.toString(7)
$46 ==>
"162013415531222510632520242612465035221121155064462525262413605341511252265440
36056624134325461423451523416401660341314"