assist/maths
This module incorporates code for various mathematical operations.
Constants
large_prime: Int = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab
A large prime number. The value is near 4 x 10^114.
Functions
base_q(n: Int, q: Int) -> List<Int>
Convert a integer n
into some base q
. This method
should scale with any integer and any logical base.
maths.base_q(123, 7)
decay(start_amt: Int, scale: Int, num: Int) -> Int
Decay some starting amount logarithmically until zero. The function evaluates
y = a - log(n)
and when n >= 2^a
the function equals zero but will return
zero whenever the result is less than the scale. This is a great
way to reduce some integer amount of something over time by incrementing n
.
maths.decay(start_amount, lovelace_scaling, datum.current_int)
effective_ratio(amt: Int, pct: Int, scale: Int) -> Int
Calculates the ratio of the amount amt
multiplied by the scale by the
percentage pct
. The scale allows for finer calculations.
maths.effective_ratio(123456789, 40, 1000) == 3086419725
from_int(self: Int) -> ByteArray
Convert a integer into a hexadecimal bytearray. This works for all integers
but odd length bytearrays will be prefixed with a zero. This function
combined with the to_int
function allows a string to represent a number
and still be used for calculations, pushing the 2^64 - 1
integer boundary.
maths.from_int(44203)
gcd(a: Int, b: Int) -> Int
Computes greatest common divisor of two numbers.
maths.gcd(20, 15)
is_in_range(n: Int, lb: Int, ub: Int) -> Bool
Verify that some integer n
is greater than the lower bound, lb
, and
less than or equal to the upper bound, ub
. The function is exclusive
for lb
but inclusive for lb + 1
.
maths.is_in_range(5, 0, 10)
legendre_symbol(a: Int, p: Int) -> Int
Calculate the Legendre symbol (a/p)
using the Euler’s criterion.
This implementation assumes that ‘a’ and ‘p’ are positive integers.
maths.legendre_symbol(10, 19)
list_powmod(lst: List<Int>, g: Int, q: Int) -> Int
Computes the power mod product of a list of integers.
maths.list_pow_mod([1,2,3], 2, 19)
list_product(lst: List<Int>) -> Int
Computes the product of a list of integers.
maths.list_product([1,2,3])
list_sum(lst: List<Int>) -> Int
Computes the sum of a list of integers.
maths.list_sum(list_of_integers)
powmod(n: Int, e: Int, q: Int) -> Int
Calculate n
to the power of e
modulo q
using the exponentiation by
squaring method. At each multiplication a modulo is calculated, allowing
very large n
and e
values.
maths.powmod(3, 2, 5)
ratio(amt: Int, pct: Int) -> Int
Calculates the ratio of the amount amt
by a percentage pct
. This can
be used to calculate rough percentages. The function ratio
is just a
special case of the effective ratio function.
maths.ratio(123, 40)
scaling(amt: Int, pct: Int) -> Int
Find the optimal scaling for a number such that it has three trailing zeros. This should be used in combination with the effective ratio for optimal calculations.
maths.scaling(123, 40)
to_int(self: ByteArray) -> Int
Convert a hexadecimal bytearray into its base 10 representation. This only works with even length bytearrays so arbitrary numbers in hexadecimal form will not in general work.
maths.to_int(#"acab")