The `gcd` method returns the greatest common divisor of two big integers.
The syntax of BigInt.gcd() method is:
BigInt gcd(BigInt other)
This gcd() method of BigInt returns the greatest common divisor of this big integer and other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the other big integer to find the greatest common divisor with |
BigInt.gcd() returns value of type BigInt
.
In this example,
num1
and num2
, initialized with 12 and 18 respectively.gcd
method to find the greatest common divisor of these numbers.void main() {
BigInt num1 = BigInt.from(12);
BigInt num2 = BigInt.from(18);
BigInt result = num1.gcd(num2);
print('GCD of 12 and 18: $result');
}
GCD of 12 and 18: 6
In this example,
num1
and num2
, initialized with -8 and 24 respectively.gcd
method to find the greatest common divisor of these numbers.void main() {
BigInt num1 = BigInt.from(-8);
BigInt num2 = BigInt.from(24);
BigInt result = num1.gcd(num2);
print('GCD of -8 and 24: $result');
}
GCD of -8 and 24: 8
In this example,
num1
and num2
, initialized with 36 and 48 respectively.gcd
method to find the greatest common divisor of these numbers.void main() {
BigInt num1 = BigInt.from(36);
BigInt num2 = BigInt.from(48);
BigInt result = num1.gcd(num2);
print('GCD of 36 and 48: $result');
}
GCD of 36 and 48: 12
In this Dart tutorial, we learned about gcd() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.