當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Dart BigInt.gcd用法及代碼示例

dart:core 庫中BigInt.gcd 方法的用法介紹如下。

用法:

BigInt gcd(
   BigInt other   
)

返回此大整數和 other 的最大公約數。

如果任一數字非零,則結果是除以 thisother 的數字上最大的整數。

最大公約數與順序無關,因此 x.gcd(y) 始終與 y.gcd(x) 相同。

對於任何整數 xx.gcd(x)x.abs()

如果thisother 都為零,則結果也為零。

例子:

print(BigInt.from(4).gcd(BigInt.from(2))); // 2
print(BigInt.from(8).gcd(BigInt.from(4))); // 4
print(BigInt.from(10).gcd(BigInt.from(12))); // 2
print(BigInt.from(10).gcd(BigInt.from(10))); // 10
print(BigInt.from(-2).gcd(BigInt.from(-3))); // 1

相關用法


注:本文由純淨天空篩選整理自dart.dev大神的英文原創作品 gcd method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。