compute bezout coefficients for 3 or more inputs
This commit is contained in:
parent
bce95536f7
commit
d507a6429d
21
cmd/gcd.go
21
cmd/gcd.go
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue