math.big #
Constants #
const zero_int = Integer{
digits: []u32{len: 0}
signum: 0
is_const: true
}
const one_int = Integer{
digits: [u32(1)]
signum: 1
is_const: true
}
const two_int = Integer{
digits: [u32(2)]
signum: 1
is_const: true
}
const three_int = Integer{
digits: [u32(3)]
signum: 1
is_const: true
}
fn bit_length #
fn bit_length(a Integer) int
bit_length returns the number of bits needed to represent the absolute value of the integer a.
fn integer_from_bytes #
fn integer_from_bytes(oinput []u8, config IntegerConfig) Integer
integer_from_bytes creates a new big.Integer
from the given byte array. By default, positive integers are assumed. If you want a negative integer, use in the following manner: value := big.integer_from_bytes(bytes, signum: -1)
fn integer_from_i64 #
fn integer_from_i64(value i64) Integer
integer_from_i64 creates a new big.Integer
from the given i64 value.
fn integer_from_int #
fn integer_from_int(value int) Integer
integer_from_int creates a new big.Integer
from the given int value.
fn integer_from_radix #
fn integer_from_radix(all_characters string, radix u32) !Integer
integer_from_radix creates a new big.Integer
from the given string and radix.
fn integer_from_string #
fn integer_from_string(characters string) !Integer
integer_from_string creates a new big.Integer
from the decimal digits specified in the given string. For other bases, use big.integer_from_radix
instead.
fn integer_from_u32 #
fn integer_from_u32(value u32) Integer
integer_from_u32 creates a new big.Integer
from the given u32 value.
fn integer_from_u64 #
fn integer_from_u64(value u64) Integer
integer_from_u64 creates a new big.Integer
from the given u64 value.
struct Integer #
struct Integer {
digits []u32
pub:
signum int
is_const bool
}
big.Integer
It has the following properties:1. Every "digit" is an integer in the range [0, 2^32).2. The signum can be one of three values: -1, 0, +1 fornegative, zero, and positive values, respectively.3. There should be no leading zeros in the digit array.4. The digits are stored in little endian format, that is,the digits with a lower positional value (towards the right when represented as a string) have a lower index, and vice versa.
fn (Integer) abs #
fn (a Integer) abs() Integer
abs returns the absolute value of the integer a
.
fn (Integer) neg #
fn (a Integer) neg() Integer
neg returns the result of negation of the integer a
.
fn (Integer) + #
fn (augend Integer) + (addend Integer) Integer
- returns the sum of the integers
augend
andaddend
.
fn (Integer) - #
fn (minuend Integer) - (subtrahend Integer) Integer
- returns the difference of the integers
minuend
andsubtrahend
fn (Integer) * #
fn (multiplicand Integer) * (multiplier Integer) Integer
- returns the product of the integers
multiplicand
andmultiplier
.
fn (Integer) div_mod #
fn (dividend Integer) div_mod(divisor Integer) (Integer, Integer)
div_mod returns the quotient and remainder from the division of the integers dividend
divided by divisor
.
WARNING: this method will panic if divisor == 0
. Refer to div_mod_checked for a safer version.
fn (Integer) div_mod_checked #
fn (dividend Integer) div_mod_checked(divisor Integer) !(Integer, Integer)
div_mod_checked returns the quotient and remainder from the division of the integers dividend
divided by divisor
. An error is returned if divisor == 0
.
fn (Integer) / #
fn (dividend Integer) / (divisor Integer) Integer
/ returns the quotient of dividend
divided by divisor
.
WARNING: this method will panic if divisor == 0
. For a division method that returns a Result refer to div_checked
.
fn (Integer) % #
fn (dividend Integer) % (divisor Integer) Integer
% returns the remainder of dividend
divided by divisor
.
WARNING: this method will panic if divisor == 0
. For a modular division method that returns a Result refer to mod_checked
.
fn (Integer) div_checked #
fn (dividend Integer) div_checked(divisor Integer) !Integer
div_checked returns the quotient of dividend
divided by divisor
or an error if divisor == 0
.
fn (Integer) mod_checked #
fn (dividend Integer) mod_checked(divisor Integer) !Integer
mod_checked returns the remainder of dividend
divided by divisor
or an error if divisor == 0
.
fn (Integer) pow #
fn (base Integer) pow(exponent u32) Integer
pow returns the integer base
raised to the power of the u32 exponent
.
fn (Integer) mod_pow #
fn (base Integer) mod_pow(exponent u32, modulus Integer) Integer
mod_pow returns the integer base
raised to the power of the u32 exponent
modulo the integer modulus
.
fn (Integer) big_mod_pow #
fn (base Integer) big_mod_pow(exponent Integer, modulus Integer) !Integer
big_mod_pow returns the integer base
raised to the power of the integer exponent
modulo the integer modulus
.
fn (Integer) inc #
fn (mut a Integer) inc()
inc increments a
by 1 in place.
fn (Integer) dec #
fn (mut a Integer) dec()
dec decrements a
by 1 in place.
fn (Integer) == #
fn (a Integer) == (b Integer) bool
== returns true
if the integers a
and b
are equal in value and sign.
fn (Integer) abs_cmp #
fn (a Integer) abs_cmp(b Integer) int
abs_cmp returns the result of comparing the magnitudes of the integers a
and b
. It returns a negative int if |a| < |b|
, 0 if |a| == |b|
, and a positive int if |a| > |b|
.
fn (Integer) < #
fn (a Integer) < (b Integer) bool
< returns true
if the integer a
is less than b
.
fn (Integer) get_bit #
fn (a Integer) get_bit(i u32) bool
get_bit checks whether the bit at the given index is set.
fn (Integer) set_bit #
fn (mut a Integer) set_bit(i u32, value bool)
set_bit sets the bit at the given index to the given value.
fn (Integer) bitwise_or #
fn (a Integer) bitwise_or(b Integer) Integer
bitwise_or returns the "bitwise or" of the integers |a|
and |b|
.
Note: both operands are treated as absolute values.
fn (Integer) bitwise_and #
fn (a Integer) bitwise_and(b Integer) Integer
bitwise_and returns the "bitwise and" of the integers |a|
and |b|
.
Note: both operands are treated as absolute values.
fn (Integer) bitwise_not #
fn (a Integer) bitwise_not() Integer
bitwise_not returns the "bitwise not" of the integer |a|
.
Note: the integer is treated as an absolute value.
fn (Integer) bitwise_xor #
fn (a Integer) bitwise_xor(b Integer) Integer
bitwise_xor returns the "bitwise exclusive or" of the integers |a|
and |b|
.
Note: both operands are treated as absolute values.
fn (Integer) lshift #
fn (a Integer) lshift(amount u32) Integer
lshift returns the integer a
shifted left by amount
bits.
fn (Integer) left_shift #
fn (a Integer) left_shift(amount u32) Integer
left_shift returns the integer a
shifted left by amount
bits.
fn (Integer) rshift #
fn (a Integer) rshift(amount u32) Integer
rshift returns the integer a
shifted right by amount
bits.
fn (Integer) right_shift #
fn (a Integer) right_shift(amount u32) Integer
right_shift returns the integer a
shifted right by amount
bits.
fn (Integer) binary_str #
fn (integer Integer) binary_str() string
binary_str returns the binary string representation of the integer a
.
fn (Integer) bin_str #
fn (integer Integer) bin_str() string
bin_str returns the binary string representation of the integer a
.
fn (Integer) hex #
fn (integer Integer) hex() string
hex returns the hexadecimal string representation of the integer a
.
fn (Integer) radix_str #
fn (integer Integer) radix_str(radix u32) string
radix_str returns the string representation of the integer a
in the specified radix.
fn (Integer) str #
fn (integer Integer) str() string
str returns the decimal string representation of the integer a
.
fn (Integer) int #
fn (a Integer) int() int
int returns the integer value of the integer a
.
Note: This may cause loss of precision.
fn (Integer) bytes #
fn (a Integer) bytes() ([]u8, int)
bytes returns the a byte representation of the integer a, along with the signum int.
Note: The byte array returned is in big endian order.
fn (Integer) factorial #
fn (a Integer) factorial() Integer
factorial returns the factorial of the integer a
.
fn (Integer) isqrt #
fn (a Integer) isqrt() Integer
isqrt returns the closest integer square root of the integer a
.
WARNING: this method will panic if a < 0
. Refer to isqrt_checked for a safer version.
fn (Integer) isqrt_checked #
fn (a Integer) isqrt_checked() !Integer
isqrt returns the closest integer square root of the integer a
. An error is returned if a < 0
.
fn (Integer) gcd #
fn (a Integer) gcd(b Integer) Integer
gcd returns the greatest common divisor of the two integers a
and b
.
fn (Integer) gcd_binary #
fn (a Integer) gcd_binary(b Integer) Integer
gcd_binary returns the greatest common divisor of the two integers a
and b
. Note that gcd_binary is faster than gcd_euclid, for large integers (over 8 bytes long). Inspired by the 2013-christmas-special by D. Lemire & R. Corderoy https://en.algorithmica.org/hpc/analyzing-performance/gcd/ For more information, refer to the Wikipedia article: https://en.wikipedia.org/wiki/Binary_GCD_algorithm Discussion and further information: https://lemire.me/blog/2013/12/26/fastest-way-to-compute-the-greatest-common-divisor/
fn (Integer) gcd_euclid #
fn (a Integer) gcd_euclid(b Integer) Integer
gcd_euclid returns the greatest common divisor of the two integers a
and b
. Note that gcd_euclid is faster than gcd_binary, for very-small-integers up to 8-byte/u64.
fn (Integer) mod_inverse #
fn (a Integer) mod_inverse(n Integer) !Integer
mod_inverse calculates the multiplicative inverse of the integer a
in the ring ℤ/nℤ
. Therefore, the return value x
satisfies a * x == 1 (mod m)
. An error is returned if a
and n
are not relatively prime, i.e. gcd(a, n) != 1
or if n <= 1
fn (Integer) is_odd #
fn (x Integer) is_odd() bool
is_odd returns true if the integer x
is odd, therefore an integer of the form 2k + 1
. An input of 0 returns false.
fn (Integer) is_power_of_2 #
fn (x Integer) is_power_of_2() bool
is_power_of_2 returns true when the integer x
satisfies 2^n
, where n >= 0
fn (Integer) bit_len #
fn (x Integer) bit_len() int
bit_len returns the number of bits required to represent the integer a
.
struct IntegerConfig #
struct IntegerConfig {
pub:
signum int = 1
}
- Constants
- fn bit_length
- fn integer_from_bytes
- fn integer_from_i64
- fn integer_from_int
- fn integer_from_radix
- fn integer_from_string
- fn integer_from_u32
- fn integer_from_u64
- struct Integer
- fn abs
- fn neg
- fn +
- fn -
- fn *
- fn div_mod
- fn div_mod_checked
- fn /
- fn %
- fn div_checked
- fn mod_checked
- fn pow
- fn mod_pow
- fn big_mod_pow
- fn inc
- fn dec
- fn ==
- fn abs_cmp
- fn <
- fn get_bit
- fn set_bit
- fn bitwise_or
- fn bitwise_and
- fn bitwise_not
- fn bitwise_xor
- fn lshift
- fn left_shift
- fn rshift
- fn right_shift
- fn binary_str
- fn bin_str
- fn hex
- fn radix_str
- fn str
- fn int
- fn bytes
- fn factorial
- fn isqrt
- fn isqrt_checked
- fn gcd
- fn gcd_binary
- fn gcd_euclid
- fn mod_inverse
- fn is_odd
- fn is_power_of_2
- fn bit_len
- struct IntegerConfig