Commit e3bfeec4 authored by Robert Griesemer's avatar Robert Griesemer

big: implemented Int.Binomial (to be used in test/hilbert.go with the

     forthcoming implementation of big.Rat)

R=rsc
CC=golang-dev
https://golang.org/cl/1229047
parent 709c5b24
...@@ -39,7 +39,7 @@ func NewInt(x int64) *Int { ...@@ -39,7 +39,7 @@ func NewInt(x int64) *Int {
} }
// Set sets z to x. // Set sets z to x and returns z.
func (z *Int) Set(x *Int) *Int { func (z *Int) Set(x *Int) *Int {
z.abs = z.abs.set(x.abs) z.abs = z.abs.set(x.abs)
z.neg = x.neg z.neg = x.neg
...@@ -47,6 +47,14 @@ func (z *Int) Set(x *Int) *Int { ...@@ -47,6 +47,14 @@ func (z *Int) Set(x *Int) *Int {
} }
// Neg sets z to -x and returns z.
func (z *Int) Neg(x *Int) *Int {
z.abs = z.abs.set(x.abs)
z.neg = len(z.abs) > 0 && !x.neg // 0 has no sign
return z
}
// Add sets z to the sum x+y and returns z. // Add sets z to the sum x+y and returns z.
func (z *Int) Add(x, y *Int) *Int { func (z *Int) Add(x, y *Int) *Int {
neg := x.neg neg := x.neg
...@@ -127,6 +135,15 @@ func (z *Int) MulRange(a, b int64) *Int { ...@@ -127,6 +135,15 @@ func (z *Int) MulRange(a, b int64) *Int {
} }
// Binomial sets z to the binomial coefficient of (n, k) and returns z.
func (z *Int) Binomial(n, k int64) *Int {
var a, b Int
a.MulRange(n-k+1, n)
b.MulRange(1, k)
return z.Quo(&a, &b)
}
// Quo sets z to the quotient x/y for y != 0 and returns z. // Quo sets z to the quotient x/y for y != 0 and returns z.
// If y == 0, a division-by-zero run-time panic occurs. // If y == 0, a division-by-zero run-time panic occurs.
// See QuoRem for more details. // See QuoRem for more details.
...@@ -237,14 +254,6 @@ func (z *Int) DivMod(x, y, m *Int) (*Int, *Int) { ...@@ -237,14 +254,6 @@ func (z *Int) DivMod(x, y, m *Int) (*Int, *Int) {
} }
// Neg computes the negation z = -x.
func (z *Int) Neg(x *Int) *Int {
z.abs = z.abs.set(x.abs)
z.neg = len(z.abs) > 0 && !x.neg // 0 has no sign
return z
}
// Cmp compares x and y and returns: // Cmp compares x and y and returns:
// //
// -1 if x < y // -1 if x < y
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment