compute bezout coefficients for 3 or more inputs

This commit is contained in:
filifa 2025-08-11 22:54:43 -04:00
parent bce95536f7
commit d507a6429d
1 changed files with 16 additions and 5 deletions

View File

@ -32,21 +32,32 @@ func gcd(cmd *cobra.Command, args []string) {
var z big.Int
var w big.Int
if extended && len(args) > 2 {
log.Fatal("--extended unsupported for more than two values")
}
coeffs := make([]big.Int, len(args))
for _, s := range args {
for i, s := range args {
_, ok := w.SetString(s, 10)
if !ok {
log.Fatal("invalid input " + s)
}
z.GCD(&x, &y, &z, &w)
for j, c := range coeffs {
if j == i {
coeffs[j].Set(&y)
} else {
coeffs[j].Mul(&c, &x)
}
}
}
if extended {
fmt.Println(&z, &x, &y)
fmt.Print(&z)
for _, c := range coeffs {
fmt.Printf(" %s", &c)
}
fmt.Println()
} else {
fmt.Println(&z)
}