mirror of
https://github.com/golang/go
synced 2024-10-03 23:21:22 -06:00
math/big: fix binaryGCD
R=rsc CC=golang-dev https://golang.org/cl/6297085
This commit is contained in:
parent
b7c5e23df0
commit
f4240666be
@ -643,12 +643,13 @@ func (z *Int) GCD(x, y, a, b *Int) *Int {
|
|||||||
return z
|
return z
|
||||||
}
|
}
|
||||||
|
|
||||||
// binaryGCD sets z to the greatest common divisor of a and b, which must be
|
// binaryGCD sets z to the greatest common divisor of a and b, which both must
|
||||||
// positive, and returns z.
|
// be > 0, and returns z.
|
||||||
// See Knuth, The Art of Computer Programming, Vol. 2, Section 4.5.2, Algorithm B.
|
// See Knuth, The Art of Computer Programming, Vol. 2, Section 4.5.2, Algorithm B.
|
||||||
func (z *Int) binaryGCD(a, b *Int) *Int {
|
func (z *Int) binaryGCD(a, b *Int) *Int {
|
||||||
u := z
|
u := z
|
||||||
v := new(Int)
|
v := new(Int)
|
||||||
|
|
||||||
// use one Euclidean iteration to ensure that u and v are approx. the same size
|
// use one Euclidean iteration to ensure that u and v are approx. the same size
|
||||||
switch {
|
switch {
|
||||||
case len(a.abs) > len(b.abs):
|
case len(a.abs) > len(b.abs):
|
||||||
@ -662,6 +663,12 @@ func (z *Int) binaryGCD(a, b *Int) *Int {
|
|||||||
v.Set(b)
|
v.Set(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// v might be 0 now
|
||||||
|
if len(v.abs) == 0 {
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
// u > 0 && v > 0
|
||||||
|
|
||||||
// determine largest k such that u = u' << k, v = v' << k
|
// determine largest k such that u = u' << k, v = v' << k
|
||||||
k := u.abs.trailingZeroBits()
|
k := u.abs.trailingZeroBits()
|
||||||
if vk := v.abs.trailingZeroBits(); vk < k {
|
if vk := v.abs.trailingZeroBits(); vk < k {
|
||||||
|
Loading…
Reference in New Issue
Block a user