BigInteger gcd() in Java

gcd(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.gcd(BigInteger val)

This method takes one argument of type BigInteger as its parameter. This method returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). Returns 0 if this == 0 && val == 0.

Parameters: One parameter is required for this method.

val: value with which the GCD is to be computed. 

Returns: GCD(abs(this), abs(val))

For Example:

BigInteger bigInteger = new BigInteger("1234")

BigInteger val = new BigInteger("156")

bigInteger.gcd(val) = > It returns 2.

Approach

Java

import java.math.BigInteger;

public class BigIntegergcd {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        BigInteger val = new BigInteger("156");
        System.out.println(bigInteger.gcd(val));
    }
}

Output:

2

No comments:

Post a Comment