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) *Int

Parameters & Attributes

NameTypeDescription
x*IntOptional destination for the first Bezout coefficient.
y*IntOptional destination for the second Bezout coefficient.
a*IntFirst input integer.
b*IntSecond 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)}
Go Reference