GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. The java.math.BigInteger.gcd(BigInteger val) method is used to calculate gcd of two BigIntegers. This method calculates gcd upon the current BigInteger by which this method is called and BigInteger passed as parameter
public BigInteger gcd(BigInteger val)
Parameters: This method accepts a parameter val which is one of the numbers out of two whose gcd is to be calculated. The number should be of BigInteger type.
Return value: This method returns a BigInteger which holds the calculated gcd of two BigIntegers.
Below program is used to illustrate the gcd() method of BigInteger.
The GCD of 54 and 42 is 6
The GCD of 4095484568135646548 and 9014548534231345454 is 2
- BigInteger add() Method in Java with Examples
- BigInteger sqrtAndRemainder() Method in Java with Examples
- Java 8 | BigInteger divideAndRemainder() method with Examples
- BigInteger divide() Method in Java with Examples
- Java 8 | BigInteger byteValueExact() method with Examples
- BigInteger multiply() Method in Java with Examples
- BigInteger intValueExact() Method in Java with Examples
- BigInteger subtract() Method in Java with Examples
- BigInteger isProbablePrime() Method in Java with Examples
- Java 8 | BigInteger shortValueExact() Method with Examples
- BigInteger nextProbablePrime() Method in Java with Examples
- Java 8 | BigInteger longValueExact() Method with Examples
- BigInteger mod() Method in Java
- BigInteger or() method in Java
- BigInteger pow() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.