Standard library / math/big
big.Int.GCD()
Compute nonnegative greatest common divisor; optional coefficient outputs satisfy a*x+b*y=gcd. Inputs may be negative or zero. This method writes to the receiver (use a separate destination to preserve inputs when needed). Expected stdout: '6 true\n'.
Formal Syntax & Structure
go syntaxgo
func (z *Int) GCD(x, y, a, b *Int) *IntParameters & Attributes
| Name | Type | Description |
|---|---|---|
| x | *Int | Optional destination for the first Bezout coefficient. |
| y | *Int | Optional destination for the second Bezout coefficient. |
| a | *Int | First input integer. |
| b | *Int | Second input integer. |
Return Value
*Int
Returns the result described above; see the example for the observed behavior.
Working Example
Example Snippetgo
package main
import ( "fmt" "math/big")
func main() { x, y := new(big.Int), new(big.Int) a, b := big.NewInt(30), big.NewInt(18) g := new(big.Int).GCD(x, y, a, b) s := new(big.Int).Add(new(big.Int).Mul(a, x), new(big.Int).Mul(b, y)) fmt.Println(g, s.Cmp(g) == 0)}