- 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
廣告