1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements signed multi-precision integers.
14 // An Int represents a signed multi-precision integer.
15 // The zero value for an Int represents the value 0.
18 abs nat
// absolute value of the integer
22 var intOne
= &Int
{false, natOne
}
31 func (x
*Int
) Sign() int {
42 // SetInt64 sets z to x and returns z.
43 func (z
*Int
) SetInt64(x
int64) *Int
{
49 z
.abs
= z
.abs
.setUint64(uint64(x
))
55 // NewInt allocates and returns a new Int set to x.
56 func NewInt(x
int64) *Int
{
57 return new(Int
).SetInt64(x
)
61 // Set sets z to x and returns z.
62 func (z
*Int
) Set(x
*Int
) *Int
{
63 z
.abs
= z
.abs
.set(x
.abs
)
69 // Abs sets z to |x| (the absolute value of x) and returns z.
70 func (z
*Int
) Abs(x
*Int
) *Int
{
71 z
.abs
= z
.abs
.set(x
.abs
)
77 // Neg sets z to -x and returns z.
78 func (z
*Int
) Neg(x
*Int
) *Int
{
79 z
.abs
= z
.abs
.set(x
.abs
)
80 z
.neg
= len(z
.abs
) > 0 && !x
.neg
// 0 has no sign
85 // Add sets z to the sum x+y and returns z.
86 func (z
*Int
) Add(x
, y
*Int
) *Int
{
90 // (-x) + (-y) == -(x + y)
91 z
.abs
= z
.abs
.add(x
.abs
, y
.abs
)
93 // x + (-y) == x - y == -(y - x)
94 // (-x) + y == y - x == -(x - y)
95 if x
.abs
.cmp(y
.abs
) >= 0 {
96 z
.abs
= z
.abs
.sub(x
.abs
, y
.abs
)
99 z
.abs
= z
.abs
.sub(y
.abs
, x
.abs
)
102 z
.neg
= len(z
.abs
) > 0 && neg
// 0 has no sign
107 // Sub sets z to the difference x-y and returns z.
108 func (z
*Int
) Sub(x
, y
*Int
) *Int
{
112 // (-x) - y == -(x + y)
113 z
.abs
= z
.abs
.add(x
.abs
, y
.abs
)
115 // x - y == x - y == -(y - x)
116 // (-x) - (-y) == y - x == -(x - y)
117 if x
.abs
.cmp(y
.abs
) >= 0 {
118 z
.abs
= z
.abs
.sub(x
.abs
, y
.abs
)
121 z
.abs
= z
.abs
.sub(y
.abs
, x
.abs
)
124 z
.neg
= len(z
.abs
) > 0 && neg
// 0 has no sign
129 // Mul sets z to the product x*y and returns z.
130 func (z
*Int
) Mul(x
, y
*Int
) *Int
{
132 // x * (-y) == -(x * y)
133 // (-x) * y == -(x * y)
134 // (-x) * (-y) == x * y
135 z
.abs
= z
.abs
.mul(x
.abs
, y
.abs
)
136 z
.neg
= len(z
.abs
) > 0 && x
.neg
!= y
.neg
// 0 has no sign
141 // MulRange sets z to the product of all integers
142 // in the range [a, b] inclusively and returns z.
143 // If a > b (empty range), the result is 1.
144 func (z
*Int
) MulRange(a
, b
int64) *Int
{
147 return z
.SetInt64(1) // empty range
148 case a
<= 0 && b
>= 0:
149 return z
.SetInt64(0) // range includes 0
151 // a <= b && (b < 0 || a > 0)
159 z
.abs
= z
.abs
.mulRange(uint64(a
), uint64(b
))
165 // Binomial sets z to the binomial coefficient of (n, k) and returns z.
166 func (z
*Int
) Binomial(n
, k
int64) *Int
{
174 // Quo sets z to the quotient x/y for y != 0 and returns z.
175 // If y == 0, a division-by-zero run-time panic occurs.
176 // See QuoRem for more details.
177 func (z
*Int
) Quo(x
, y
*Int
) *Int
{
178 z
.abs
, _
= z
.abs
.div(nil, x
.abs
, y
.abs
)
179 z
.neg
= len(z
.abs
) > 0 && x
.neg
!= y
.neg
// 0 has no sign
184 // Rem sets z to the remainder x%y for y != 0 and returns z.
185 // If y == 0, a division-by-zero run-time panic occurs.
186 // See QuoRem for more details.
187 func (z
*Int
) Rem(x
, y
*Int
) *Int
{
188 _
, z
.abs
= nat(nil).div(z
.abs
, x
.abs
, y
.abs
)
189 z
.neg
= len(z
.abs
) > 0 && x
.neg
// 0 has no sign
194 // QuoRem sets z to the quotient x/y and r to the remainder x%y
195 // and returns the pair (z, r) for y != 0.
196 // If y == 0, a division-by-zero run-time panic occurs.
198 // QuoRem implements T-division and modulus (like Go):
200 // q = x/y with the result truncated to zero
203 // (See Daan Leijen, ``Division and Modulus for Computer Scientists''.)
205 func (z
*Int
) QuoRem(x
, y
, r
*Int
) (*Int
, *Int
) {
206 z
.abs
, r
.abs
= z
.abs
.div(r
.abs
, x
.abs
, y
.abs
)
207 z
.neg
, r
.neg
= len(z
.abs
) > 0 && x
.neg
!= y
.neg
, len(r
.abs
) > 0 && x
.neg
// 0 has no sign
212 // Div sets z to the quotient x/y for y != 0 and returns z.
213 // If y == 0, a division-by-zero run-time panic occurs.
214 // See DivMod for more details.
215 func (z
*Int
) Div(x
, y
*Int
) *Int
{
216 y_neg
:= y
.neg
// z may be an alias for y
230 // Mod sets z to the modulus x%y for y != 0 and returns z.
231 // If y == 0, a division-by-zero run-time panic occurs.
232 // See DivMod for more details.
233 func (z
*Int
) Mod(x
, y
*Int
) *Int
{
235 if z
== y ||
alias(z
.abs
, y
.abs
) {
251 // DivMod sets z to the quotient x div y and m to the modulus x mod y
252 // and returns the pair (z, m) for y != 0.
253 // If y == 0, a division-by-zero run-time panic occurs.
255 // DivMod implements Euclidean division and modulus (unlike Go):
257 // q = x div y such that
258 // m = x - y*q with 0 <= m < |q|
260 // (See Raymond T. Boute, ``The Euclidean definition of the functions
261 // div and mod''. ACM Transactions on Programming Languages and
262 // Systems (TOPLAS), 14(2):127-144, New York, NY, USA, 4/1992.
265 func (z
*Int
) DivMod(x
, y
, m
*Int
) (*Int
, *Int
) {
267 if z
== y ||
alias(z
.abs
, y
.abs
) {
284 // Cmp compares x and y and returns:
290 func (x
*Int
) Cmp(y
*Int
) (r
int) {
291 // x cmp y == x cmp y
294 // (-x) cmp (-y) == -(x cmp y)
310 func (x
*Int
) String() string {
315 return s
+ x
.abs
.string(10)
319 func fmtbase(ch
int) int {
334 // Format is a support routine for fmt.Formatter. It accepts
335 // the formats 'b' (binary), 'o' (octal), 'd' (decimal) and
336 // 'x' (hexadecimal).
338 func (x
*Int
) Format(s fmt
.State
, ch
int) {
342 fmt
.Fprint(s
, x
.abs
.string(fmtbase(ch
)))
346 // Int64 returns the int64 representation of z.
347 // If z cannot be represented in an int64, the result is undefined.
348 func (x
*Int
) Int64() int64 {
353 if _W
== 32 && len(x
.abs
) > 1 {
354 v |
= int64(x
.abs
[1]) << 32
363 // SetString sets z to the value of s, interpreted in the given base,
364 // and returns z and a boolean indicating success. If SetString fails,
365 // the value of z is undefined.
367 // If the base argument is 0, the string prefix determines the actual
368 // conversion base. A prefix of ``0x'' or ``0X'' selects base 16; the
369 // ``0'' prefix selects base 8, and a ``0b'' or ``0B'' prefix selects
370 // base 2. Otherwise the selected base is 10.
372 func (z
*Int
) SetString(s
string, base
int) (*Int
, bool) {
373 if len(s
) == 0 || base
< 0 || base
== 1 ||
16 < base
{
378 if neg || s
[0] == '+' {
386 z
.abs
, _
, scanned
= z
.abs
.scan(s
, base
)
387 if scanned
!= len(s
) {
390 z
.neg
= len(z
.abs
) > 0 && neg
// 0 has no sign
396 // SetBytes interprets b as the bytes of a big-endian, unsigned integer and
397 // sets z to that value.
398 func (z
*Int
) SetBytes(b
[]byte) *Int
{
400 z
.abs
= z
.abs
.make((len(b
) + s
- 1) / s
)
406 for i
:= s
; i
> 0; i
-- {
408 w |
= Word(b
[len(b
)-i
])
419 for i
:= len(b
); i
> 0; i
-- {
421 w |
= Word(b
[len(b
)-i
])
433 // Bytes returns the absolute value of x as a big-endian byte array.
434 func (z
*Int
) Bytes() []byte {
436 b
:= make([]byte, len(z
.abs
)*s
)
438 for i
, w
:= range z
.abs
{
439 wordBytes
:= b
[(len(z
.abs
)-i
-1)*s
: (len(z
.abs
)-i
)*s
]
440 for j
:= s
- 1; j
>= 0; j
-- {
441 wordBytes
[j
] = byte(w
)
447 for i
< len(b
) && b
[i
] == 0 {
455 // BitLen returns the length of the absolute value of z in bits.
456 // The bit length of 0 is 0.
457 func (z
*Int
) BitLen() int {
458 return z
.abs
.bitLen()
462 // Exp sets z = x**y mod m. If m is nil, z = x**y.
463 // See Knuth, volume 2, section 4.6.3.
464 func (z
*Int
) Exp(x
, y
, m
*Int
) *Int
{
465 if y
.neg ||
len(y
.abs
) == 0 {
477 z
.abs
= z
.abs
.expNN(x
.abs
, y
.abs
, mWords
)
478 z
.neg
= len(z
.abs
) > 0 && x
.neg
&& y
.abs
[0]&1 == 1 // 0 has no sign
483 // GcdInt sets d to the greatest common divisor of a and b, which must be
485 // If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y.
486 // If either a or b is not positive, GcdInt sets d = x = y = 0.
487 func GcdInt(d
, x
, y
, a
, b
*Int
) {
503 Y
:= new(Int
).SetInt64(1)
505 lastX
:= new(Int
).SetInt64(1)
513 q
, r
= q
.QuoRem(A
, B
, r
)
542 // ProbablyPrime performs n Miller-Rabin tests to check whether z is prime.
543 // If it returns true, z is prime with probability 1 - 1/4^n.
544 // If it returns false, z is not prime.
545 func ProbablyPrime(z
*Int
, n
int) bool {
546 return !z
.neg
&& z
.abs
.probablyPrime(n
)
550 // Rand sets z to a pseudo-random number in [0, n) and returns z.
551 func (z
*Int
) Rand(rnd
*rand
.Rand
, n
*Int
) *Int
{
553 if n
.neg
== true ||
len(n
.abs
) == 0 {
557 z
.abs
= z
.abs
.random(rnd
, n
.abs
, n
.abs
.bitLen())
562 // ModInverse sets z to the multiplicative inverse of g in the group ℤ/pℤ (where
563 // p is a prime) and returns z.
564 func (z
*Int
) ModInverse(g
, p
*Int
) *Int
{
566 GcdInt(&d
, z
, nil, g
, p
)
567 // x and y are such that g*x + p*y = d. Since p is prime, d = 1. Taking
568 // that modulo p results in g*x = 1, therefore x is the inverse element.
576 // Lsh sets z = x << n and returns z.
577 func (z
*Int
) Lsh(x
*Int
, n
uint) *Int
{
578 z
.abs
= z
.abs
.shl(x
.abs
, n
)
584 // Rsh sets z = x >> n and returns z.
585 func (z
*Int
) Rsh(x
*Int
, n
uint) *Int
{
587 // (-x) >> s == ^(x-1) >> s == ^((x-1) >> s) == -(((x-1) >> s) + 1)
588 t
:= z
.abs
.sub(x
.abs
, natOne
) // no underflow because |x| > 0
590 z
.abs
= t
.add(t
, natOne
)
591 z
.neg
= true // z cannot be zero if x is negative
595 z
.abs
= z
.abs
.shr(x
.abs
, n
)
601 // And sets z = x & y and returns z.
602 func (z
*Int
) And(x
, y
*Int
) *Int
{
605 // (-x) & (-y) == ^(x-1) & ^(y-1) == ^((x-1) | (y-1)) == -(((x-1) | (y-1)) + 1)
606 x1
:= nat
{}.sub(x
.abs
, natOne
)
607 y1
:= nat
{}.sub(y
.abs
, natOne
)
608 z
.abs
= z
.abs
.add(z
.abs
.or(x1
, y1
), natOne
)
609 z
.neg
= true // z cannot be zero if x and y are negative
614 z
.abs
= z
.abs
.and(x
.abs
, y
.abs
)
621 x
, y
= y
, x
// & is symmetric
624 // x & (-y) == x & ^(y-1) == x &^ (y-1)
625 y1
:= nat
{}.sub(y
.abs
, natOne
)
626 z
.abs
= z
.abs
.andNot(x
.abs
, y1
)
632 // AndNot sets z = x &^ y and returns z.
633 func (z
*Int
) AndNot(x
, y
*Int
) *Int
{
636 // (-x) &^ (-y) == ^(x-1) &^ ^(y-1) == ^(x-1) & (y-1) == (y-1) &^ (x-1)
637 x1
:= nat
{}.sub(x
.abs
, natOne
)
638 y1
:= nat
{}.sub(y
.abs
, natOne
)
639 z
.abs
= z
.abs
.andNot(y1
, x1
)
645 z
.abs
= z
.abs
.andNot(x
.abs
, y
.abs
)
651 // (-x) &^ y == ^(x-1) &^ y == ^(x-1) & ^y == ^((x-1) | y) == -(((x-1) | y) + 1)
652 x1
:= nat
{}.sub(x
.abs
, natOne
)
653 z
.abs
= z
.abs
.add(z
.abs
.or(x1
, y
.abs
), natOne
)
654 z
.neg
= true // z cannot be zero if x is negative and y is positive
658 // x &^ (-y) == x &^ ^(y-1) == x & (y-1)
659 y1
:= nat
{}.add(y
.abs
, natOne
)
660 z
.abs
= z
.abs
.and(x
.abs
, y1
)
666 // Or sets z = x | y and returns z.
667 func (z
*Int
) Or(x
, y
*Int
) *Int
{
670 // (-x) | (-y) == ^(x-1) | ^(y-1) == ^((x-1) & (y-1)) == -(((x-1) & (y-1)) + 1)
671 x1
:= nat
{}.sub(x
.abs
, natOne
)
672 y1
:= nat
{}.sub(y
.abs
, natOne
)
673 z
.abs
= z
.abs
.add(z
.abs
.and(x1
, y1
), natOne
)
674 z
.neg
= true // z cannot be zero if x and y are negative
679 z
.abs
= z
.abs
.or(x
.abs
, y
.abs
)
686 x
, y
= y
, x
// | is symmetric
689 // x | (-y) == x | ^(y-1) == ^((y-1) &^ x) == -(^((y-1) &^ x) + 1)
690 y1
:= nat
{}.sub(y
.abs
, natOne
)
691 z
.abs
= z
.abs
.add(z
.abs
.andNot(y1
, x
.abs
), natOne
)
692 z
.neg
= true // z cannot be zero if one of x or y is negative
697 // Xor sets z = x ^ y and returns z.
698 func (z
*Int
) Xor(x
, y
*Int
) *Int
{
701 // (-x) ^ (-y) == ^(x-1) ^ ^(y-1) == (x-1) ^ (y-1)
702 x1
:= nat
{}.sub(x
.abs
, natOne
)
703 y1
:= nat
{}.sub(y
.abs
, natOne
)
704 z
.abs
= z
.abs
.xor(x1
, y1
)
710 z
.abs
= z
.abs
.xor(x
.abs
, y
.abs
)
717 x
, y
= y
, x
// ^ is symmetric
720 // x ^ (-y) == x ^ ^(y-1) == ^(x ^ (y-1)) == -((x ^ (y-1)) + 1)
721 y1
:= nat
{}.sub(y
.abs
, natOne
)
722 z
.abs
= z
.abs
.add(z
.abs
.xor(x
.abs
, y1
), natOne
)
723 z
.neg
= true // z cannot be zero if only one of x or y is negative
728 // Not sets z = ^x and returns z.
729 func (z
*Int
) Not(x
*Int
) *Int
{
731 // ^(-x) == ^(^(x-1)) == x-1
732 z
.abs
= z
.abs
.sub(x
.abs
, natOne
)
737 // ^x == -x-1 == -(x+1)
738 z
.abs
= z
.abs
.add(x
.abs
, natOne
)
739 z
.neg
= true // z cannot be zero if x is positive