Although the standard library of Erlang has become more and more complete, I have found that the support for various common algorithms is far from enough. This is an Erlang algorithm library similar to the <algorithm>
in C++/STL, which is intended to supplement the shortcomings of the standard library.
fun ealgo_ntl:prime_list/1
: Generates a prime list where any element is not greater thanN
.fun ealgo_ntl:prime_array/1
: Generates a prime array where any element is not greater thanN
.fun ealgo_ntl:divisible/2
: Gives true ifN
is divisible byM
, and false otherwise.fun ealgo_ntl:is_odd/1
: Gives true ifN
is an odd integer, and false otherwise.fun ealgo_ntl:is_even/1
: Gives true ifN
is an even integer, and false otherwise.fun ealgo_ntl:multiplicity/2
: Gives the highest power ofA
that dividesN
.fun ealgo_ntl:quotient/2
: Gives the integer quotient ofN
andM
.fun ealgo_ntl:remainder/2
: Gives the remainder on division ofN
byM
.fun ealgo_ntl:quotient_remainder/2
: Gives the quotient and remainder from division ofN
byM
.fun ealgo_ntl:gcd/2
: Gives the greatest common divisor ofN
andM
. note: we definegcd(0, 0) = 0
for convenience.fun ealgo_ntl:gcd/1
: Gives the greatest common divisor of a list of integers. If the list has only one integerX
, it returnsgcd(X, 0)
; If the list is empty, for this corner case, it returns0
.fun ealgo_ntl:lcm/2
: Gives the least common multiple ofN
andM
. note: we definelcm(X, Y) = 0
whenX = 0
ory = 0
.fun ealgo_ntl:lcm/1
: Gives the least common multiple of a list of integers. If the list has only one integerX
, it returnslcm(X, 1)
; If the list is empty, for this corner case, it returns1
.fun ealgo_ntl:power/2
: GivesA
to the powerX
whereA
is an integer andX
is a nonnegative integer. note:power(0, 0)
is undefined.fun ealgo_ntl:power_mod/3
: GivesA ^ X mod M
whereA
is an integer,X
is a nonnegative integer andM
is a nonzero integer. note:power_mod(0, 0, M)
is undefined.fun ealgo_ntl:is_coprime/2
: Gives true ifN
andM
are relatively prime, and false otherwise. relation:is_coprime(N, M) = true <=> gcd(N, M) = 1
fun ealgo_ntl:jacobi_symbol/2
: Gives the Jacobi symbol(A / N)
..fun ealgo_ntl:is_prime/1
: Gives true ifN
is a prime number, and false otherwise, mainly implemented with miller rabin test and strong lucas test.fun ealgo_ntl:bit_length/1
: Gives the number of binary bits necessary to represent the integerN
. In other words, the integerS
satisfied that2^(S-1) ≤ |N| < 2^S
, and we definedS = 0
whenN = 0
for the corner case.fun ealgo_ntl:nthrootrem/2
: Gives theN'th
root and remainder of integerX
.fun ealgo_ntl:extended_gcd/2
: Gives the extended greatest common divisor of N and M. note:extended_gcd(0, 0) = {0, {0, 0}}
for convenience.
fun ealgo:cartesian_product/1
: Gives the cartesian product of a list of list.fun ealgo:combinations/1
: Gives all combinations ofL
.fun ealgo:combinations/2
: Gives all combinations ofL
containing exactlyN
elements.fun ealgo:permutations/1
: Gives all permutations ofL
.fun ealgo:permutations/2
: Gives all permutations ofL
containing exactlyN
elements.fun ealgo:next_permutation/1
: Gives the next permutation ofL
.
fun ealgo:shuffle/1
: Shuffle the listL
randomly.fun ealgo:select_by_weight/1
: Select one element from listL
by weight.fun ealgo:select_amount_by_weight/2
: SelectN
element from listL
by weight.fun ealgo:get_rand_elem/1
: Select one element from listL
randomly.fun ealgo:get_rand_elems/2
: SelectN
element from listL
randomly.
fun ealgo_shield:create/1
: Create a trie tree from a given list of shielding words.fun ealgo_shield:insert/2
: Insert a shielding word into the shielding trie.fun ealgo_shield:detect/2
: Detect whether the sentence contains a shielding word.fun ealgo_shield:lookup/2
: Judge whether the sentence has a prefix is a shielding word.
fun ealgo:rabin_karp/2
: Rabin Karp gives a list of the starting character positions at whichPattern
appears as a substring ofString
.fun ealgo:sgn/1
: Sign Function gives-1
,0
, or1
depending on whetherX
is negative, zero, or positive.fun ealgo:boole/1
: Indicator Function yields1
ifX
is true and0
if it is false.fun ealgo:ustep/1
: Heaviside Step Function Represents the unit step function, equal to0
forX < 0
and1
forX >= 0
.fun ealgo:id/1
: Identity Function GivesX
(the identity operation).