BigInteger gcd(BigInteger val)

优质
小牛编辑
139浏览
2023-12-01

描述 (Description)

java.math.BigInteger.gcd(BigInteger val)返回一个BigInteger,其值是abs(this)和abs(val)的最大公约数。 如果= = 0 && val == 0,则返回0。

声明 (Declaration)

以下是java.math.BigInteger.gcd()方法的声明。

public BigInteger gcd(BigInteger val)

参数 (Parameters)

val - 计算GCD的值。

返回值 (Return Value)

此方法返回一个BigInteger,其值为GCD(abs(this),abs(val))。

异常 (Exception)

NA

例子 (Example)

以下示例显示了math.BigInteger.gcd()方法的用法。

package cn.xnip;
import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      // create 3 BigInteger objects
      BigInteger bi1, bi2, bi3;
      // assign values to bi1, bi2
      bi1 = new BigInteger("18");
      bi2 = new BigInteger("24");
      // assign gcd of bi1, bi2 to bi3
      bi3 = bi1.gcd(bi2);
      String str = "GCD of " + bi1 + " and " + bi2 + " is " +bi3;
      // print bi3 value
      System.out.println( str );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

GCD of 18 and 24 is 6