You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-algorithms/numerical/gcd.go

48 lines
581 B
Go

package gcd
func gcd(x uint, y uint) uint {
var shift uint = 0
if x == y {
return x
}
if x == 0 {
return y
}
if y == 0 {
return x
}
for shift := 0; (x | y) & 1 == 0; shift++ {
x = x >> 1
y = y >> 1
}
for ; (x & 1) == 0 ; {
x = x >> 1
}
for ; y == 0 ; {
for ; (y & 1) == 0 ; {
y = y >> 1
}
if x > y {
t := x
x = y
y = t
}
y = y - x
}
y = y << shift
return y
}