# BigInteger.GreatestCommonDivisor Method (BigInteger, BigInteger)

.NET Framework (current version)

Note

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Finds the greatest common divisor of two BigInteger values.

Namespace:   System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

## Syntax

```Public Shared Function GreatestCommonDivisor (
left As BigInteger,
right As BigInteger
) As BigInteger
```

#### Parameters

left
Type: System.Numerics.BigInteger

The first value.

right
Type: System.Numerics.BigInteger

The second value.

#### Return Value

Type: System.Numerics.BigInteger

The greatest common divisor of left and right.

## Remarks

The greatest common divisor is the largest number into which the two BigInteger values can be divided without returning a remainder.

If the left and right parameters are non-zero numbers, the method always returns at least a value of 1 because all numbers can be divided by 1. If either parameter is zero, the method returns the absolute value of the non-zero parameter. If both values are zero, the method returns zero.

Note

Computing the greatest common divisor of very large values of left and right can be a very time-consuming operation.

The value returned by the GreatestCommonDivisor method is always positive regardless of the sign of the left and right parameters.

## Examples

The following example illustrates a call to the GreatestCommonDivisor method and the exception handling necessary to provide useful information about an ArgumentOutOfRangeException. The result indicates that the greatest common divisor of these two numbers is 1.

```Dim n1 As BigInteger = BigInteger.Pow(154382190, 3)
Dim n2 As BigInteger = BigInteger.Multiply(1643590, 166935)
Try
Console.WriteLine("The greatest common divisor of {0} and {1} is {2}.", _
n1, n2, BigInteger.GreatestCommonDivisor(n1, n2))
Catch e As ArgumentOutOfRangeException
Console.WriteLine("Unable to calculate the greatest common divisor:")
Console.WriteLine("   {0} is an invalid value for {1}", _
e.ActualValue, e.ParamName)
End Try
```

## Version Information

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone
Available since 8.1