bigint
import "std/types" (included in import "std") · namespace std.types
Whole numbers of any size: cryptography, exact factorials and combinatorics, amounts in the smallest unit past 2^63, and IDs from other systems. A BigInt is immutable, and the operators work on it as on a long, with whole numbers mixing in on the right.
grav
import "std"
fn main() {
let a = BigInt.parse("123456789012345678901234567890")
print(a * a + BigInt.one())
print(BigInt.of(2).pow(128))
print(BigInt.factorial(25))
print(BigInt.binomial(60, 30))
print("${a / 1000} ${a % 97} ${a > BigInt.of(1)}")
print("${BigInt.of(-7) / BigInt.of(2)} ${BigInt.of(-7) % BigInt.of(2)} ${BigInt.of(-7).mod(BigInt.of(2))}")
print("${BigInt.parse("ff_ff", 16)} ${BigInt.of(255).toString(2)}")
print(BigInt.of(7).modPow(BigInt.of(560), BigInt.of(561)))
print(BigInt.parse("170141183460469231731687303715884105727").isProbablePrime())
print(BigInt.of(10).pow(40).sqrt())
}15241578753238836750495351562536198787501905199875019052101
340282366920938463463374607431768211456
15511210043330985984000000
118264581564861424
123456789012345678901234567 52 true
-3 -1 1
65535 11111111
1
true
100000000000000000000- Arithmetic:
+ - * / %(andplus,minus,times,div,rem,divRemfor both at once),negate,abs,pow(e),sqrt(the integer root),gcd./and%truncate toward zero and the remainder takes the dividend's sign, as forlong;mod(m)is never negative. Division by zero is anArithmeticError. - Modular arithmetic:
modPow(e, m)(a negativeeuses the inverse),modInverse(m), andisProbablePrime(rounds)(Miller–Rabin; exact below about 3.3 × 10^24 with the default 12 rounds). - Bits:
shiftLeft,shiftRight(rounding toward negative infinity, as>>),bitLength,testBit, andand,or,xorfor non-negative values. - Conversions:
BigInt.of(long),parse(text, base)(bases 2 to 36, an optional sign,_between digits),tryParse,toString(base),toHex,toLongandtoInt(anArithmeticErrorwhen the value does not fit;fitsLong()asks first), andtoDouble. - Speed: the digits are 32-bit limbs handled in C. Multiplication is schoolbook below 40 limbs and Karatsuba above, division is Knuth's algorithm D,
factorialandbinomialmultiply in a balanced product tree, andmodPowreduces after every step instead of building the huge power.
API
class BigInt
An integer of any size.
Constructors
new BigInt()
Static members
static fn of(v: long) -> BigInt— The value of a long.static fn zero() -> BigInt— 0 and 1.static fn one() -> BigIntstatic fn parse(text: string, base: int = 10) -> BigInt— Parses digits inbase(2 to 36; an optional sign; '_' between digits allowed); ValueError when the text is not a number.static fn tryParse(text: string, base: int = 10) -> BigInt?— parse, or null when the text is not a number.static fn factorial(n: int) -> BigInt— n! (a product tree: fast for large n).static fn binomial(n: int, k: int) -> BigInt— n choose k.
Methods
fn sign() -> int— -1, 0 or 1.fn isZero() -> boolfn isNegative() -> boolfn isEven() -> boolfn fitsLong() -> bool— Whether it fits in a long.fn toLong() -> long— The value as a long; ArithmeticError when it does not fit.fn toInt() -> int— The value as an int; ArithmeticError when it does not fit.fn toDouble() -> double— The nearest double (infinity past its range).fn toString() -> string— Decimal digits.fn toString(base: int) -> string— Digits inbase(2 to 36, lowercase letters).fn toHex() -> string— Hex digits (no prefix; a '-' for negative values).fn bitLength() -> int— The number of bits in the magnitude (0 for 0).fn testBit(i: int) -> bool— Whether bitiof the magnitude is set.fn hash() -> long— A hash of the value (equal values hash alike).fn plus(o: BigInt) -> BigIntfn minus(o: BigInt) -> BigIntfn times(o: BigInt) -> BigIntfn div(o: BigInt) -> BigInt— The quotient, toward zero; ArithmeticError for division by zero.fn rem(o: BigInt) -> BigInt— The remainder of div (the dividend's sign).fn divRem(o: BigInt) -> Array<BigInt>— [quotient, remainder] from one division.fn mod(m: BigInt) -> BigInt— The remainder in [0, |m|): modular arithmetic's mod.fn negate() -> BigIntfn abs() -> BigIntfn pow(e: int) -> BigInt— self to the powere(e >= 0).fn modPow(e: BigInt, m: BigInt) -> BigInt— self^e mod m (e >= 0, m > 0), without the huge intermediate power.fn modInverse(m: BigInt) -> BigInt— x with self * x ≡ 1 (mod m); ArithmeticError when there is none.fn gcd(o: BigInt) -> BigInt— The greatest common divisor (never negative).fn sqrt() -> BigInt— The integer square root: the largest r with r*r <= self (self >= 0).fn isProbablePrime(rounds: int = 12) -> bool— Miller–Rabin with the firstroundsprimes as witnesses (12 is exact below 3.3 * 10^24; beyond that a composite passes with odds under 4^-rounds).fn shiftLeft(k: int) -> BigInt— self * 2^k.fn shiftRight(k: int) -> BigInt— self / 2^k, rounded toward negative infinity (as >> on a long).fn and(o: BigInt) -> BigInt— Bitwise and, or and xor of non-negative values; ValueError for negative ones.fn or(o: BigInt) -> BigIntfn xor(o: BigInt) -> BigIntfn compareTo(o: BigInt) -> int— Negative, zero or positive as self is below, equal to or above o.fn equals(o: BigInt) -> boolfn max(o: BigInt) -> BigIntfn min(o: BigInt) -> BigIntfn operator+(o: BigInt) -> BigIntfn operator-(o: BigInt) -> BigIntfn operator*(o: BigInt) -> BigIntfn operator/(o: BigInt) -> BigIntfn operator%(o: BigInt) -> BigIntfn operator+(v: long) -> BigIntfn operator-(v: long) -> BigIntfn operator*(v: long) -> BigIntfn operator/(v: long) -> BigIntfn operator%(v: long) -> BigIntfn operator==(o: BigInt) -> boolfn operator<(o: BigInt) -> boolfn operator<=(o: BigInt) -> boolfn operator>(o: BigInt) -> boolfn operator>=(o: BigInt) -> boolfn operator==(v: long) -> boolfn operator<(v: long) -> boolfn operator<=(v: long) -> boolfn operator>(v: long) -> boolfn operator>=(v: long) -> bool