Big integer is a special integer which holds the larger value than primitive data types like int or long. Basic operations on big int is explained in part1.
In this blog post, it includes the advanced operations like
GCD, prime number generation, modular arithmetic and primality testing.
GCD:
Greatest common divisor(It gives you the largest positive integer that can divide
two or more integers without remainder)
Prime number generation:
Prime number is a natural number which can be divided by
itself and by 1.
Modular arithmetic:
It is modulus operation which gives you the remainder.
Primality testing:
It is an
algorithm to determine the input is prime.
Here is the program.
import java.math.BigInteger;
public class advancebigIntEg {
public static void main(String[] args) {
BigInteger x = new
BigInteger("345216723456123457");
BigInteger y = new
BigInteger("897645324123412345");
// Let us calculate GCD
BigInteger gcdcalc = x.gcd(y);
System.out.println("The GCD value is: " +
gcdcalc);
// It is the next prime number generation coe
BigInteger nextPrimeno = x.nextProbablePrime();
System.out.println("Next prime number is: " +
nextPrimeno);
//Modulus operation
BigInteger modvalue = x.mod(y);
System.out.println("Modulus value is: " +
modvalue);
// Primality testing code is given here
boolean primeNo = x.isProbablePrime(1);
if (primeNo == true)
System.out.println("It is a prime number");
else
System.out.println("It is not a prime number");
}
}
The output is given below.
C:\raji\blog>javac advancebigIntEg.java
C:\raji\blog>java advancebigIntEg
The GCD value is: 1
Next prime number is: 345216723456123473
Modulus value is: 345216723456123457
It is not a prime number
Note : Big integer is always
take time compared to primitive data types. But it suitable for complex
number operations on large numbers. So choose it wisely according to your
applications.
No comments:
Post a Comment