bignum
stableArbitrary-precision integer arithmetic using base-10^9 digit arrays, supporting addition, subtraction, multiplication, division, power, GCD, and comparisons on numbers of any size.
use plugin bignum::{from_string, from_int, add, …} Functions (12)
- from_string Parses a decimal string (optionally prefixed with + or -) into a BigNum.
- from_int Creates a BigNum from a native Zolo integer.
- add Adds two BigNum values and returns a new BigNum.
- sub Subtracts b from a and returns a new BigNum.
- mul Multiplies two BigNum values using schoolbook multiplication.
- div Integer division truncated toward zero.
- rem Returns the remainder after integer division.
- pow Raises a BigNum to a non-negative integer exponent using fast exponentiation.
- gcd Computes the greatest common divisor of two BigNums using Euclidean algorithm.
- compare Returns -1 if a < b, 0 if a == b, or 1 if a > b.
- to_string Converts a BigNum back to its decimal string representation.
- digit_count Returns the number of decimal digits in the BigNum (excluding sign).
Parses a decimal string (optionally prefixed with + or -) into a BigNum.
Parses a decimal string (optionally prefixed with + or -) into a BigNum. Use this when you have a large number that exceeds native integer range.
use plugin bignum::{from_string, to_string, add}
let a = from_string("999999999999999999999999999999")
let b = from_string("1")
let result = add(a, b)
print(to_string(result))
Creates a BigNum from a native Zolo integer.
Creates a BigNum from a native Zolo integer. Useful when starting arithmetic that will grow beyond native int bounds.
use plugin bignum::{from_int, mul, to_string}
let n = from_int(1000000)
let squared = mul(n, n)
print(to_string(squared))
Adds two BigNum values and returns a new BigNum.
Adds two BigNum values and returns a new BigNum. Handles mixed signs correctly.
use plugin bignum::{from_string, add, to_string}
let x = from_string("123456789012345678901234567890")
let y = from_string("987654321098765432109876543210")
print(to_string(add(x, y)))
Subtracts b from a and returns a new BigNum.
Subtracts b from a and returns a new BigNum.
use plugin bignum::{from_string, sub, to_string}
let x = from_string("1000000000000000000000")
let y = from_string("1")
print(to_string(sub(x, y)))
Multiplies two BigNum values using schoolbook multiplication.
Multiplies two BigNum values using schoolbook multiplication.
use plugin bignum::{from_int, mul, to_string}
let factorial = from_int(1)
let i = from_int(1)
let limit = from_int(20)
// compute 20! manually
print(to_string(mul(from_int(2432902008176640000), from_int(1))))
Integer division truncated toward zero.
Integer division truncated toward zero. Raises an error on division by zero.
use plugin bignum::{from_string, div, to_string}
let a = from_string("1000000000000000000000")
let b = from_string("7")
print(to_string(div(a, b)))
Returns the remainder after integer division.
Returns the remainder after integer division. Sign matches the dividend.
use plugin bignum::{from_string, rem, to_string}
let a = from_string("1000000000000000000007")
let b = from_string("7")
print(to_string(rem(a, b)))
Raises a BigNum to a non-negative integer exponent using fast exponentiation.
Raises a BigNum to a non-negative integer exponent using fast exponentiation.
use plugin bignum::{from_int, pow, to_string, digit_count}
let two = from_int(2)
let p = pow(two, 100)
print("2^100 has {digit_count(p)} digits: {to_string(p)}")
Computes the greatest common divisor of two BigNums using Euclidean algorithm.
Computes the greatest common divisor of two BigNums using Euclidean algorithm.
use plugin bignum::{from_string, gcd, to_string}
let a = from_string("123456789012345678901234567890")
let b = from_string("987654321098765432109876543210")
print(to_string(gcd(a, b)))
Returns -1 if a < b, 0 if a == b, or 1 if a > b.
Returns -1 if a < b, 0 if a == b, or 1 if a > b. Useful for sorting or conditional logic.
use plugin bignum::{from_string, compare}
let a = from_string("999999999999999")
let b = from_string("1000000000000000")
let ord = compare(a, b)
print("order: {ord}")
Converts a BigNum back to its decimal string representation.
Converts a BigNum back to its decimal string representation.
use plugin bignum::{from_string, pow, to_string}
let base = from_string("10")
let result = pow(base, 50)
print(to_string(result))
Returns the number of decimal digits in the BigNum (excluding sign).
Returns the number of decimal digits in the BigNum (excluding sign).
use plugin bignum::{from_string, pow, from_int, digit_count}
let two = from_int(2)
let p = pow(two, 1000)
print("2^1000 has {digit_count(p)} decimal digits")