bigint
Whole numbers of unlimited size: large factorials, cryptography, and any arithmetic that will not fit in 64 bits.
import bigint
An i64 holds about nine quintillion. For anything larger, such as the factorial of 30 or a cryptographic key, use bigint.Int, whose size is limited only by memory.
Most functions return a new number and leave their arguments alone. The ones whose names say To, From or By, such as AddTo, change the number in place and are faster in a loop because they take no new memory. These numbers allocate; release the ones you no longer need with Free.
Types
Int
struct IntFromInt or Parse.| Name | Type | Description |
|---|---|---|
neg | bool = false | True when the number is negative. |
mag | Vector | The absolute value, as 32-bit limbs from least to most significant. |
import bigint
func main:
mut a := bigint.FromInt(123456789)
mut b := bigint.Pow(bigint.FromInt(2), 100)
defer a.free()
defer b.free()
println b.to_str()
println a.is_zero(), a.sign(), a.is_odd(), a.is_even()
println b.bit_len(), b.limb_count()
c := a.clone()
println c.to_str()
endfree
func free()The example for this item is shown with the ones above: bigint-Int.salam
is_zero
func is_zero(): boolThe example for this item is shown with the ones above: bigint-Int.salam
sign
func sign(): intThe example for this item is shown with the ones above: bigint-Int.salam
is_odd
func is_odd(): boolThe example for this item is shown with the ones above: bigint-Int.salam
is_even
func is_even(): boolThe example for this item is shown with the ones above: bigint-Int.salam
bit_len
func bit_len(): intThe example for this item is shown with the ones above: bigint-Int.salam
limb_count
func limb_count(): intThe example for this item is shown with the ones above: bigint-Int.salam
clone
func clone(): IntThe example for this item is shown with the ones above: bigint-Int.salam
to_str
func to_str(): strThe example for this item is shown with the ones above: bigint-Int.salam
Functions
And
func And(a: Int, b: Int): Intimport bigint
func main:
a := bigint.FromInt(12)
b := bigint.FromInt(10)
println bigint.ToStr(bigint.And(a, b)), bigint.ToStr(bigint.Or(a, b)), bigint.ToStr(bigint.Xor(a, b))
println bigint.ToStr(bigint.AndNot(a, b)), bigint.ToStr(bigint.Not(a))
println bigint.PopCount(a), bigint.TrailingZeros(a)
endOr
func Or(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
Xor
func Xor(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
AndNot
func AndNot(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
Not
func Not(x: Int): IntThe example for this item is shown with the ones above: bigint-And.salam
PopCount
func PopCount(x: Int): intThe example for this item is shown with the ones above: bigint-And.salam
TrailingZeros
func TrailingZeros(x: Int): intThe example for this item is shown with the ones above: bigint-And.salam
Zero
func Zero(): Intimport bigint
func main:
a := bigint.FromInt(9000000000000000000)
b := bigint.FromUint(18000000000000000000 as u64)
println bigint.ToStr(bigint.Add(a, b))
println bigint.ToStr(bigint.Sub(a, b))
println bigint.ToStr(bigint.Mul(a, b))
println bigint.ToStr(bigint.Sqr(a))
println bigint.ToStr(bigint.AddInt(a, 1)), bigint.ToStr(bigint.SubInt(a, 1)), bigint.ToStr(bigint.MulInt(a, 2))
println bigint.ToStr(bigint.Neg(a)), bigint.ToStr(bigint.Abs(bigint.Neg(a)))
println bigint.ToStr(bigint.Shl(bigint.One(), 70)), bigint.ToStr(bigint.Shr(a, 60))
println bigint.ToStr(bigint.Zero()), bigint.ToStr(bigint.Pow10(20))
endOne
func One(): IntThe example for this item is shown with the ones above: bigint-Add.salam
FromInt
func FromInt(v: i64): Inti64.The example for this item is shown with the ones above: bigint-Int.salam
FromUint
func FromUint(v: u64): Intu64.The example for this item is shown with the ones above: bigint-Add.salam
Clone
func Clone(x: Int): Intimport bigint
func main:
a := bigint.FromInt(5)
b := bigint.FromInt(-9)
println bigint.Cmp(a, b), bigint.CmpAbs(a, b)
println bigint.Equals(a, a), bigint.Less(a, b), bigint.Greater(a, b)
println bigint.ToStr(bigint.Min(a, b)), bigint.ToStr(bigint.Max(a, b))
println bigint.EqualsInt(a, 5), bigint.IsZero(a), bigint.Sign(b)
println bigint.IsOdd(a), bigint.IsEven(a), bigint.BitLen(a), bigint.Bit(a, 1)
c := bigint.Clone(b)
println bigint.ToStr(c)
endFree
func Free(x &: Int)import bigint
func main:
mut acc := bigint.Zero()
defer bigint.Free(acc)
bigint.SetInt(acc, 10)
bigint.AddTo(acc, bigint.FromInt(5))
bigint.SubFrom(acc, bigint.FromInt(3))
bigint.MulBy(acc, bigint.FromInt(4))
println bigint.ToStr(acc)
bigint.AddIntTo(acc, 2)
bigint.MulIntBy(acc, 10)
println bigint.ToStr(acc)
bigint.NegateIn(acc)
bigint.ShlBy(acc, 1)
println bigint.ToStr(acc)
bigint.ShrBy(acc, 2)
println bigint.ToStr(acc)
bigint.Set(acc, bigint.FromInt(7))
println bigint.ToStr(acc)
endIsZero
func IsZero(x: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Sign
func Sign(x: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
BitLen
func BitLen(x: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
Bit
func Bit(x: Int, i: int): inti of the absolute value, zero or one.The example for this item is shown with the ones above: bigint-Cmp.salam
IsOdd
func IsOdd(x: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
IsEven
func IsEven(x: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Cmp
func Cmp(a: Int, b: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
CmpAbs
func CmpAbs(a: Int, b: Int): intThe example for this item is shown with the ones above: bigint-Cmp.salam
Equals
func Equals(a: Int, b: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Less
func Less(a: Int, b: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Greater
func Greater(a: Int, b: Int): boolThe example for this item is shown with the ones above: bigint-Cmp.salam
Min
func Min(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Cmp.salam
Max
func Max(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Cmp.salam
EqualsInt
func EqualsInt(a: Int, v: i64): booli64.The example for this item is shown with the ones above: bigint-Cmp.salam
ToStr
func ToStr(x: Int): strThe example for this item is shown with the ones above: bigint-Add.salam
ToStrBase
func ToStrBase(x: Int, base: int): strimport bigint
func main:
mut ok := false
n := bigint.Parse("123456789012345678901234567890", ok)
println ok, bigint.ToStr(n)
h := bigint.ParseBase("ff", 16, ok)
println ok, bigint.ToStr(h), bigint.ToStrBase(h, 2), bigint.ToHex(n)
println bigint.ToStr(bigint.ParseOr("xyz", 10))
mut fits := false
v := bigint.ToInt(h, fits)
println v, fits
println bigint.ToIntClamped(n), bigint.ToFloat(n)
endToHex
func ToHex(x: Int): strThe example for this item is shown with the ones above: bigint-Parse.salam
Parse
func Parse(s: str, ok &: bool): Intok turns false.The example for this item is shown with the ones above: bigint-Parse.salam
ParseBase
func ParseBase(s: str, base: int, ok &: bool): IntThe example for this item is shown with the ones above: bigint-Parse.salam
ParseOr
func ParseOr(s: str, base: int): IntThe example for this item is shown with the ones above: bigint-Parse.salam
ToInt
func ToInt(x: Int, ok &: bool): i64i64; when it does not fit, ok turns false.The example for this item is shown with the ones above: bigint-Parse.salam
ToIntClamped
func ToIntClamped(x: Int): i64i64, giving the largest or smallest possible value when it does not fit.The example for this item is shown with the ones above: bigint-Parse.salam
ToFloat
func ToFloat(x: Int): f64f64; the least significant digits may be lost.The example for this item is shown with the ones above: bigint-Parse.salam
ByteLen
func ByteLen(x: Int): intimport bigint
import mem
func main:
n := bigint.FromInt(65536)
size := bigint.ByteLen(n)
println size
buf := mem.Allocate(size as u64)
println bigint.ToBytes(n, buf, size)
back := bigint.FromBytes(buf, size)
println bigint.ToStr(back)
mem.Free(buf)
endFromBytes
func FromBytes(p: void*, n: int): Intn bytes in memory, most significant first.The example for this item is shown with the ones above: bigint-Bytes.salam
ToBytes
func ToBytes(x: Int, out: void*, n: int): booln bytes of memory, most significant first; it returns false when it does not fit.The example for this item is shown with the ones above: bigint-Bytes.salam
Pow
func Pow(x: Int, e: int): Inte.The example for this item is shown with the ones above: bigint-Int.salam
Pow10
func Pow10(n: int): Intn.The example for this item is shown with the ones above: bigint-Add.salam
ModExp
func ModExp(base: Int, e: Int, m: Int): Intbase to the power e modulo m quickly, without building the huge intermediate number; the basis of RSA.import bigint
func main:
println bigint.ToStr(bigint.Factorial(30))
println bigint.ToStr(bigint.ModExp(bigint.FromInt(4), bigint.FromInt(13), bigint.FromInt(497)))
println bigint.ToStr(bigint.Gcd(bigint.FromInt(48), bigint.FromInt(18))), bigint.ToStr(bigint.Lcm(bigint.FromInt(4), bigint.FromInt(6)))
mut x := bigint.Zero()
mut y := bigint.Zero()
g := bigint.ExtGcd(bigint.FromInt(240), bigint.FromInt(46), x, y)
println bigint.ToStr(g), bigint.ToStr(x), bigint.ToStr(y)
mut ok := false
inv := bigint.ModInverse(bigint.FromInt(3), bigint.FromInt(11), ok)
println ok, bigint.ToStr(inv)
println bigint.ToStr(bigint.Sqrt(bigint.Pow10(20)))
endGcd
func Gcd(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-ModExp.salam
Lcm
func Lcm(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-ModExp.salam
ExtGcd
func ExtGcd(a: Int, b: Int, x &: Int, y &: Int): Intx and y with a·x + b·y equal to the gcd.The example for this item is shown with the ones above: bigint-ModExp.salam
ModInverse
func ModInverse(a: Int, m: Int, ok &: bool): Inta modulo m is one. When there is none, ok turns false.The example for this item is shown with the ones above: bigint-ModExp.salam
Sqrt
func Sqrt(x: Int): IntThe example for this item is shown with the ones above: bigint-ModExp.salam
Factorial
func Factorial(n: int): Intn, with no size limit.The example for this item is shown with the ones above: bigint-ModExp.salam
Neg
func Neg(x: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Abs
func Abs(x: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Add
func Add(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Sub
func Sub(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Mul
func Mul(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
Sqr
func Sqr(x: Int): IntThe example for this item is shown with the ones above: bigint-Add.salam
AddInt
func AddInt(a: Int, v: i64): IntThe example for this item is shown with the ones above: bigint-Add.salam
SubInt
func SubInt(a: Int, v: i64): IntThe example for this item is shown with the ones above: bigint-Add.salam
MulInt
func MulInt(a: Int, v: i64): IntThe example for this item is shown with the ones above: bigint-Add.salam
Shl
func Shl(x: Int, k: int): Intk bits, that is, multiplies by 2 to the power k.The example for this item is shown with the ones above: bigint-Add.salam
Shr
func Shr(x: Int, k: int): Intk bits.The example for this item is shown with the ones above: bigint-Add.salam
Set
func Set(x &: Int, src: Int)src into x.The example for this item is shown with the ones above: bigint-AddTo.salam
SetInt
func SetInt(x &: Int, v: i64)x.The example for this item is shown with the ones above: bigint-AddTo.salam
AddTo
func AddTo(x &: Int, y: Int)y to x, changing x itself.The example for this item is shown with the ones above: bigint-AddTo.salam
SubFrom
func SubFrom(x &: Int, y: Int)y from x.The example for this item is shown with the ones above: bigint-AddTo.salam
MulBy
func MulBy(x &: Int, y: Int)x by y.The example for this item is shown with the ones above: bigint-AddTo.salam
AddIntTo
func AddIntTo(x &: Int, v: i64)x.The example for this item is shown with the ones above: bigint-AddTo.salam
MulIntBy
func MulIntBy(x &: Int, v: i64)x by an ordinary whole number.The example for this item is shown with the ones above: bigint-AddTo.salam
NegateIn
func NegateIn(x &: Int)x.The example for this item is shown with the ones above: bigint-AddTo.salam
ShlBy
func ShlBy(x &: Int, k: int)x left in place.The example for this item is shown with the ones above: bigint-AddTo.salam
ShrBy
func ShrBy(x &: Int, k: int)x right in place.The example for this item is shown with the ones above: bigint-AddTo.salam
DivMod
func DivMod(a: Int, b: Int, q &: Int, r &: Int): boolq and r together; the quotient is rounded towards zero. It returns false when the divisor is zero.import bigint
func main:
a := bigint.FromInt(-17)
b := bigint.FromInt(5)
println bigint.ToStr(bigint.Div(a, b)), bigint.ToStr(bigint.Rem(a, b))
println bigint.ToStr(bigint.FloorDiv(a, b)), bigint.ToStr(bigint.FloorMod(a, b)), bigint.ToStr(bigint.Mod(a, b))
mut q := bigint.Zero()
mut r := bigint.Zero()
println bigint.DivMod(a, b, q, r), bigint.ToStr(q), bigint.ToStr(r)
println bigint.FloorDivMod(a, b, q, r), bigint.ToStr(q), bigint.ToStr(r)
println bigint.DivMod(a, bigint.Zero(), q, r)
mut rem := 0 as i64
println bigint.ToStr(bigint.DivModInt(bigint.FromInt(100), 7, rem)), rem, bigint.ModInt(bigint.FromInt(100), 7)
endDiv
func Div(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Div.salam
Rem
func Rem(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Div.salam
FloorDivMod
func FloorDivMod(a: Int, b: Int, q &: Int, r &: Int): boolDivMod, but the quotient is rounded down and the remainder takes the sign of the divisor, as in Python.The example for this item is shown with the ones above: bigint-Div.salam
FloorDiv
func FloorDiv(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Div.salam
FloorMod
func FloorMod(a: Int, b: Int): IntThe example for this item is shown with the ones above: bigint-Div.salam
Mod
func Mod(a: Int, m: Int): Intm, for modular arithmetic.The example for this item is shown with the ones above: bigint-Div.salam
DivModInt
func DivModInt(a: Int, v: i64, rem &: i64): Intrem.The example for this item is shown with the ones above: bigint-Div.salam
ModInt
func ModInt(a: Int, v: i64): i64The example for this item is shown with the ones above: bigint-Div.salam