Java BigInteger Tutorial with Example

By Arvind Rai, November 20, 2023
This page will walk through Java BigInteger tutorial with examples. BigInteger is an immutable arbitrary-precision integer. It performs the operations of java.lang.Math class and many more operations such as modular arithmetic, GCD calculation and prime generation etc. We can instantiate BigInteger and can access its value using following methods.
BigInteger bint = new BigInteger("215");
bint.intValue();
bint.longValue();
bint.floatValue();
bint.doubleValue(); 
Now find the examples for BigInteger methods in detail.

1. probablePrime()

probablePrime() returns a positive BigInteger that is probably prime. We need to pass bit length that will be the bit length of returned BigInteger. We will also pass instance of Random class.
ProbablePrimeDemo.java
package com.concretepage;
import java.math.BigInteger;
import java.util.Random;
public class ProbablePrimeDemo {
	public static void main(String[] args) {
		BigInteger bint = BigInteger.probablePrime(8, new Random());
		System.out.println("Probable Prime Number: "+ bint);
	}
}
Output
Probable Prime Number: 223 

2. nextProbablePrime()

It returns the next BigInteger that is probably prime. It never skips a prime number while looking for next prime number.
NextProbablePrimeDemo.java
package com.concretepage;
import java.math.BigInteger;
import java.util.Random;
public class NextProbablePrimeDemo {
	public static void main(String[] args) {
		BigInteger bint = BigInteger.probablePrime(8, new Random());
		System.out.println("Probable Prime Number: "+ bint);
		System.out.println("Next Probable Prime Number: "+ bint.nextProbablePrime());
	}
} 
Output
Probable Prime Number: 251
Next Probable Prime Number: 257 

3. isProbablePrime()

It returns true if value is probably prime otherwise false. We need to pass an integer value as certainty which is a measure of the uncertainty that the caller is willing to tolerate. If certainty is <=0, then the method isProbablePrime() will always return true.
IsProbablePrimeDemo.java
package com.concretepage;
import java.math.BigInteger;
public class IsProbablePrimeDemo {
	public static void main(String[] args) {
		BigInteger bint1 = new BigInteger("195");
		BigInteger bint2 = new BigInteger("197");
		BigInteger bint3 = new BigInteger("190");
		System.out.println("bint1.isProbablePrime(2):"+ bint1.isProbablePrime(2));
		System.out.println("bint2.isProbablePrime(2):"+ bint2.isProbablePrime(2));
		System.out.println("bint3.isProbablePrime(2):"+ bint3.isProbablePrime(2));
		System.out.println("bint3.isProbablePrime(-2):"+ bint3.isProbablePrime(-2));
	}
} 
Output
bint1.isProbablePrime(2):false
bint2.isProbablePrime(2):true
bint3.isProbablePrime(2):false
bint3.isProbablePrime(-2):true 

4. add()

add() method adds two BigInteger instances.
AddDemo.java
package com.concretepage;
import java.math.BigInteger;
public class AddDemo {
	public static void main(String[] args) {
		BigInteger bint = new BigInteger("234");
		BigInteger bintRes = bint.add(new BigInteger("450"));
		System.out.println("Result:" + bintRes);
	}
} 
Output
Result:684 

5. subtract()

subtract() method subtracts the given BigInteger from calling BigInteger instance.
SubtractDemo.java
package com.concretepage;
import java.math.BigInteger;
public class SubtractDemo {
	public static void main(String[] args) {
		BigInteger bint = new BigInteger("500");
		BigInteger bintRes = bint.subtract(new BigInteger("300"));
		System.out.println("Result:" + bintRes);
	}
} 
Output
Result:200 

6. multiply()

multiply() multiplies two BigInteger instances.
MultiplyDemo.java
package com.concretepage;
import java.math.BigInteger;
public class MultiplyDemo {
	public static void main(String[] args) {
		BigInteger bint = new BigInteger("50");
		BigInteger bintRes = bint.multiply(new BigInteger("30"));
		System.out.println("Result:" + bintRes);
	}
} 
Output
Result:1500 

7. divide()

divide() method divides the calling BigInteger by given BigInteger instance.
DivideDemo.java
package com.concretepage;
import java.math.BigInteger;
public class DivideDemo {
	public static void main(String[] args) {
		BigInteger bint = new BigInteger("500");
		BigInteger bintRes = bint.divide(new BigInteger("25"));
		System.out.println("Result:" + bintRes);
	}
} 
Output
Result:20 

8. remainder()

It returns the BigInteger as remainder of the operation a % b.
RemainderDemo.java
package com.concretepage;
import java.math.BigInteger;
public class RemainderDemo {
	public static void main(String[] args) {
		int a = 500;
		int b = 28;
		BigInteger bint = new BigInteger(String.valueOf(a));
		BigInteger bintRes = bint.remainder(new BigInteger(String.valueOf(b)));
		System.out.println("a % b: " + bintRes);
	}
} 
Output
a % b: 24 

9. divideAndRemainder()

It returns an array of BigInteger of the result of divide() and remainder() method for the two BigInteger instances.
DivideAndRemainderDemo.java
package com.concretepage;
import java.math.BigInteger;
public class DivideAndRemainderDemo {
	public static void main(String[] args) {
		int a = 597;
		int b = 20;
		BigInteger bint = new BigInteger(String.valueOf(a));
		BigInteger[] bintRes = bint.divideAndRemainder(new BigInteger(String.valueOf(b)));
		System.out.println("a/b: " + bintRes[0]);
		System.out.println("a % b: " + bintRes[1]);
	}
} 
Output
a/b: 29
a % b: 17 


10. pow()

It calculates the power of a BigInteger value and returns a BigInteger. The exponent will be an integer.
PowDemo.java
package com.concretepage;
import java.math.BigInteger;
public class PowDemo {
	public static void main(String[] args) {
		int exponent = 7;
		BigInteger bint = new BigInteger("12");
		BigInteger bintRes = bint.pow(exponent);
		System.out.println("Result: " + bintRes);
	}
} 
Output
Result: 35831808 

11. gcd()

gcd() is greatest common divisor of absolute values of two BigInteger and returns a BigInteger.
GCDDemo.java
package com.concretepage;
import java.math.BigInteger;
public class GCDDemo {
	public static void main(String[] args) {
		BigInteger bint1 = new BigInteger("24");
		BigInteger bint2 = new BigInteger("40");
		BigInteger bintRes = bint1.gcd(bint2);
		System.out.println("GCD:" + bintRes);
	}
} 
Output
GCD:8 

12. abs()

It returns a BigInteger with absolute value of a given BigInteger.
AbsDemo.java
package com.concretepage;
import java.math.BigInteger;
public class AbsDemo {
	public static void main(String[] args) {
		BigInteger bint1 = new BigInteger("-234");
		System.out.println(bint1.abs());
		BigInteger bint2 = new BigInteger("100");
		System.out.println(bint2.abs());
	}
} 
Output
234
100 

13. negate()

It returns a BigInteger with negative value of the original value.
NegateDemo.java
package com.concretepage;
import java.math.BigInteger;
public class NegateDemo {
	public static void main(String[] args) {
		BigInteger bint1 = new BigInteger("-134");
		System.out.println("negate() of -134 :"+bint1.negate());
		BigInteger bint2 = new BigInteger("155");
		System.out.println("negate() of 155 :" + bint2.negate());
	}
} 
Output
negate() of -134 :134
negate() of 155 :-155 

14. signum()

It returns the signum function values of the BigInteger. Signum function value for negative values is -1 and signum function for 0 is 0 and signum function for positive value is +1.
SignumDemo.java
package com.concretepage;
import java.math.BigInteger;
public class SignumDemo {
	public static void main(String[] args) {
		//-1 for values < 0
		System.out.println("signum for -15: "+ new BigInteger("-15").signum());
		//0 for value 0
		System.out.println("signum for   0: "+ new BigInteger("0").signum());
		//1 for values > 0
		System.out.println("signum for  25: "+ new BigInteger("25").signum());
	}
} 
Output
signum for -15: -1
signum for   0: 0
signum for  25: 1 

15. mod()

Modulus is the remainder of the Euclidean division . mod() always returns BigInteger with positive value whereas remainder() can return BigInteger with +ve and -ve values both. In this example we will use mod() and remainder() both to understand the differences between the two.
ModDemo.java
package com.concretepage;
import java.math.BigInteger;
public class ModDemo {
	public static void main(String[] args) {
		System.out.println("32 mod 7= "+new BigInteger("32").mod(new BigInteger("7")));
		System.out.println("32 % 7= "+ new BigInteger("32").remainder(new BigInteger("7")));
		
		System.out.println("-32 mod 7= "+new BigInteger("-32").mod(new BigInteger("7")));
		System.out.println("-32 % 7= "+ new BigInteger("-32").remainder(new BigInteger("7")));
	}
} 
Output
32 mod 7= 4
32 % 7= 4
-32 mod 7= 3
-32 % 7= -4 

16. modPow()

This method first calculates pow() then applies mod(). Here exponent is a BigInteger and can have -ve value.
ModPowDemo.java
package com.concretepage;
import java.math.BigInteger;
public class ModPowDemo {
	public static void main(String[] args) {
		BigInteger b = new BigInteger("-2");
		BigInteger e = new BigInteger("3");
		BigInteger m = new BigInteger("5");
		BigInteger res= b.modPow(e, m);
		System.out.println("b.modPow(e, m): " +res);
		//The result is equivalent to below code
		System.out.println("b.pow(e.intValue()).mod(m): " + b.pow(e.intValue()).mod(m));
	}
} 
Output
b.modPow(e, m): 2
b.pow(e.intValue()).mod(m): 2 

17. modInverse()

modInverse() method will calculate modulus of the inverse of the given value as (this^-1 mod m). According to java doc, it will throw error if "BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m)."
ModInverseDemo.java
package com.concretepage;
import java.math.BigInteger;
public class ModInverseDemo {
	public static void main(String[] args) {
		BigInteger b1 = new BigInteger("-13");
		BigInteger b2 = new BigInteger("4");
		System.out.println("b1.modInverse(b2): "+b1.modInverse(b2));
	}
}
Output
b1.modInverse(b2): 3 

18. compareTo()

This method compares the BigInteger with given BigInteger and returns -1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than given value.
CompareToDemo.java
package com.concretepage;
import java.math.BigInteger;
public class CompareToDemo {
	public static void main(String[] args) {
		System.out.println(new BigInteger("300").compareTo(new BigInteger("150")));
		System.out.println(new BigInteger("200").compareTo(new BigInteger("350")));
		System.out.println(new BigInteger("140").compareTo(new BigInteger("140")));
	}
} 
Output
1
-1
0 

19. equals()

This method checks for equality of a BigInteger with a given BigInteger.
EqualsDemo.java
package com.concretepage;
import java.math.BigInteger;
public class EqualsDemo {
	public static void main(String[] args) {
		System.out.println(new BigInteger("300").equals(new BigInteger("150")));
		System.out.println(new BigInteger("140").equals(new BigInteger("140")));
	}
} 
Output
false
true 

20 min() and max()

min() returns the BigInteger with minimum value and max() returns the BigInteger with maximum value between the two BigInteger instances.
MinMaxDemo.java
package com.concretepage;
import java.math.BigInteger;
public class MinMaxDemo {
	public static void main(String[] args) {
		System.out.println("Min: "+ new BigInteger("300").min(new BigInteger("150")));
		System.out.println("Max: "+new BigInteger("300").max(new BigInteger("150")));
	}
} 
Output
Min: 150
Max: 300 

21. Reference

Java Doc: Class BigInteger
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us