Home  >  Core Java

Java BigInteger Tutorial with Example

By Arvind Rai, September 14, 2016
This page will walk through java BigInteger tutorial with example. 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 as follows.
BigInteger bint = new BigInteger("215");
bint.intValue();
bint.longValue();
bint.floatValue();
bint.doubleValue(); 
Now find the examples for BigInteger methods in detail.


BigInteger probablePrime() Example

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 

BigInteger nextProbablePrime() Example

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 

BigInteger isProbablePrime() Example

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 

BigInteger add() Example

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 

BigInteger subtract() Example

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 

BigInteger multiply() Example

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 

BigInteger divide() Example

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 

BigInteger remainder() Example

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 

BigInteger divideAndRemainder() Example

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 

BigInteger pow() Example

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 

BigInteger gcd() Example

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 

BigInteger abs() Example

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 

BigInteger negate() Example

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 

BigInteger signum() Example

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 

BigInteger mod() Example

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 

BigInteger modPow() Example

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 

BigInteger modInverse() Example

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 

BigInteger compareTo() Example

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 

BigInteger equals() Example

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 

BigInteger min() and max() Example

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 

Reference

Java Doc: Class BigInteger
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us