Beispiel #1
0
// GCD sets z to the greatest common divisor of a and b, which must be positive
// numbers, and returns z. If x and y are not nil, GCD sets x and y such that
// z = a*x + b*y. If either a or b is not positive, GCD sets z = x = y = 0.
func (z *Int) GCD(x, y, a, b *Int) *Int {
	z.doinit()

	// Compatibility with math/big
	if a.Cmp(intZero) <= 0 || b.Cmp(intZero) <= 0 {
		z.Set(intZero)
		return z
	}

	// allow for nil x and y
	var x_ptr C.mpz_ptr = nil
	if x != nil {
		x.doinit()
		x_ptr = x.ptr
	}
	var y_ptr C.mpz_ptr = nil
	if y != nil {
		y.doinit()
		y_ptr = y.ptr
	}

	a.doinit()
	b.doinit()
	C.mpz_gcdext(z.ptr, x_ptr, y_ptr, a.ptr, b.ptr)
	return z
}
Beispiel #2
0
Datei: int.go Projekt: locusf/gmp
// GCD sets z to the greatest common divisor of a and b, which both must
// be > 0, and returns z.
// If x and y are not nil, GCD sets x and y such that z = a*x + b*y.
// If either a or b is <= 0, GCD sets z = x = y = 0.
func (z *Int) GCD(x, y, a, b *Int) *Int {
	z.doinit()
	a.doinit()
	b.doinit()
	if a.Sign() <= 0 || b.Sign() <= 0 {
		z.SetInt64(0)
		if x != nil {
			x.SetInt64(0)
		}
		if y != nil {
			y.SetInt64(0)
		}
	} else if x == nil && y == nil {
		C.mpz_gcd(&z.i[0], &a.i[0], &b.i[0])
	} else {
		if x != nil {
			x.doinit()
		} else {
			x = _Int_0
		}
		if y != nil {
			y.doinit()
		} else {
			y = _Int_0
		}
		C.mpz_gcdext(&z.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0])
	}
	return z
}
Beispiel #3
0
// GcdInt sets d to the greatest common divisor of a and b,
// which must be positive numbers.
// If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y.
// If either a or b is not positive, GcdInt sets d = x = y = 0.
func GcdInt(d, x, y, a, b *Int) {
	d.doinit()
	x.doinit()
	y.doinit()
	a.doinit()
	b.doinit()
	C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0])
}