- Java.math 软件包附加信息
- Java.math - 枚举
- Java.math - 讨论
Java.math.BigInteger.gcd() 方法
说明
java.math.BigInteger.gcd(BigInteger val) 返回一个 BigInteger,其值为 abs(this) 和 abs(val) 的最大公约数。如果 this == 0 && val == 0,则返回 0。
声明
以下是 java.math.BigInteger.gcd() 方法的声明。
public BigInteger gcd(BigInteger val)
public BigInteger gcd(BigInteger val)
val − 要计算 GCD 的值。
返回值
此方法返回一个 BigInteger,其值为 GCD(abs(this), abs(val))。
异常
NA
示例
以下示例演示了 math.BigInteger.gcd() 方法的用法。
package com.tutorialspoint;
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
java_math_biginteger.htm
广告