View Source Caustic.Utils (Caustic v0.1.25)
A collection of useful methods in cryptography and number theory.
Link to this section Summary
Functions
Checks whether an integer is abundant.
An integer n
is abundant if and only if σ(n) - n > n
.
The other number of an amicable pair, if any. Returns nil
if doesn't
have such pair.
Decode a base58-encoded string into its hexadecimal string representation.
Encodes an integer into its base58 representation. If given a string, by default it will interpret the string as a hex.
Same as base58_decode but outputs an integer instead of hex string.
Returns checksum, payload, and version
Encodes a binary to its base58check representation.
Decodes a MIME Base64 encoded string.
Gets the character code of a MIME base64 digit.
Encodes a string into its MIME Base64 representation.
Encodes an integer codepoint 0 <= n
<= 63 to its MIME Base64 character.
Lists the exponents of n
when n
is represented as binary.
For example, 34
in binary is 100010
or 2^5 + 2^1
, so
the function will return [1, 5]
.
Converts a Bitcoin 256-bit private key to the Wallet Import Format. Defaults to outputting compressed format.
Converts a bitstring (including binary) into array of 0s and 1s. For simple binary you can also use :binary.decode_unsigned
Interprets a bitstring (including binary) as an unsigned integer. You can use :binary.decode_unsigned/1 if it's a normal binary.
Checks whether an integer n > 1
is a composite number. 1 is a unit,
neither a prime nor composite. Will return false on n <= 1
.
Checks whether an integer is deficient.
An integer n
is abundant if and only if σ(n) - n < n
.
Gets all the positive divisors of an integer.
Counts how many positive divisors an integer has. d(n)
.
Sums the positive divisors of an integer. σ(n)
.
The sum of the e
-th power of the positive divisors of an integer. σ_e(n)
.
For example, the divisors of 15 are 1, 3, 5, and 15, so
σ_2(15) = 1 + 3^2 + 5^2 + 15^2 = 260
Find the prime factors of an integer.
Find the prime factors and their exponents of an integer.
Finds the smallest prime divisor of a number and the result of dividing by that prime. The factorization of 1 is 1 1 and 0 is 0 1.
Find the greatest common divisor of two integers.
Find the greatest common divisor d
of two integers a
and b
, while also finding
the coefficients x
and y
such that ax + by = d
.
Guess the base of an integer string using its prefix. Defaults to 10, and doesn't check for validity of the digits.
Calculate the document hash, used for ECDSA.
Removes hexadecimal prefix from a string.
Solves the linear congruence ax = b (mod m).
Splits a list into its first n
elements and the rest.
The base 2 logarithm of an integer and its remainder. For example, 9 = 2^3 + 1, so log2i(9) = 3 with remainder 1.
Calculate the md5 hash.
Finds the least residue of a number modulo m
.
Find the modular inverse (modular multiplicative inverse).
Finds the first integer n
where n >= from
satisfying some property.
The smallest number t
such that a^t = 1 (mod m)
.
Determines whether an integer is a perfect number.
A number n
is perfect iff σ(n) - n = n
.
Determines whether an integer is a k-perfect number.
A number n
is k-perfect iff σ(n) = kn
.
Perfect numbers are 2-perfect.
Calculates integer exponentiation. Exponent can be negative.
Fast exponentiation modulo m. Calculates x^y mod m.
The i
-th prime, starting at index 0.
Checks whether an integer is a prime.
Checks whether an integer is a prime using Sieve of Eratosthenes algorithm. Don't use on very large numbers.
Find all primes p ≤ n
using Sieve of Eratosthenes method.
The first n
primes.
Checks whether a number n
is a primitive root modulo m
.
Finds the primitive roots of a positive integer m
.
a
is a primitive root of an integer m
if a
is a least residue
and has order (mod m
) of t = φ(n)
.
This means that t
is the smallest number such that a^t = 1 (mod m)
.
Gets all the positive divisors of a number, excluding the number itself.
Counts how many proper divisors an integer has. d(n) - 1
.
Removes the first element of a list.
Examples
iex> Caustic.Utils.select([[:a, :b], [1, 2, 3]])
[[:a, 1], [:a, 2], [:a, 3], [:b, 1], [:b, 2], [:b, 3]]
iex> Caustic.Utils.select([[["hello"], ["bye"]], ["world"]])
[[["hello"], "world"], [["bye"], "world"]]
Examples
iex> Caustic.Utils.smallest_prime_divisor(2)
2
iex> Caustic.Utils.smallest_prime_divisor(4)
2
iex> Caustic.Utils.smallest_prime_divisor(144)
2
iex> Caustic.Utils.smallest_prime_divisor(5)
5
iex> Caustic.Utils.smallest_prime_divisor(35)
5
If n
is a square number, return its root. Otherwise nil
.
Creates a lazy stream of integers satisfying some property.
Finds the index of an ASCII character inside a string. Not unicode friendly!
Finds all subsets of a set (represented by a keyword list).
Finds all subsets with cardinality n
of a set (represented by a keyword list).
Determines whether an integer is a superperfect number.
A number n
is superperfect if and only if σ(σ(n)) = 2n
.
Find the digits of a nonnegative integer n
in a particular base
.
Parse a string which can be in any base to integer, autodetecting the base using the prefix.
Parse a string which can be in any base to integer, specifying the base.
Convert an integer into its string representation in any base
.
If an integer can be written as the sum of the squares of two positive integers, return those two integers {a, b} where a <= b.
Euler's totient function φ(n)
.
The number of positive integers less than or equal to m
and relatively prime to m
.
Positive integers less than or equal to m
and relatively prime to m
.
Checks whether an integer is triangular.
An integer is triangular if and only if it can be expressed as n(n + 1)/2
for some integer n
.
Finds the nonnegative integer m
such that m(m + 1)/2 = n
.
Returns nil
if no such number exists.
Link to this section Functions
Checks whether an integer is abundant.
An integer n
is abundant if and only if σ(n) - n > n
.
examples
Examples
iex> Caustic.Utils.abundant?(6)
false
The other number of an amicable pair, if any. Returns nil
if doesn't
have such pair.
examples
Examples
iex> Caustic.Utils.amicable_pair(220)
284
iex> Caustic.Utils.amicable_pair(2)
nil
Decode a base58-encoded string into its hexadecimal string representation.
examples
Examples
iex> Caustic.Utils.base58_decode("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn")
"0x801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff"
iex> Caustic.Utils.base58_decode("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn", prefix: false)
"801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff"
Encodes an integer into its base58 representation. If given a string, by default it will interpret the string as a hex.
If given hex and it has leading zeros, then each byte of zeros will be encoded as 1.
examples
Examples
iex> Caustic.Utils.base58_encode("801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff")
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58_encode(<<57>>, convert_from_hex: false)
"z"
iex> Caustic.Utils.base58_encode(63716817338599314535577169638518475271320430400871647684951348108655027767484127754748927)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58_encode("0x000001")
"112"
Same as base58_decode but outputs an integer instead of hex string.
Returns checksum, payload, and version
examples
Examples
iex> Caustic.Utils.base58check_decode "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
{:ok, <<196, 126, 131, 255>>, "1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", :private_key_wif}
iex> Caustic.Utils.base58check_decode "1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy"
{:ok, <<55, 254, 252, 208>>, "bbc1e42a39d05a4cc61752d6963b7f69d09bb27b", :address}
Encodes a binary to its base58check representation.
Possible values for version: :address
, :address_p2sh
, :address_testnet
,
private_key_wif
, private_key_bip38_encrypted
, public_key_bit32_extended
Can also use custom binary version.
examples
Examples
iex> Caustic.Utils.base58check_encode("0x1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", :private_key_wif)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58check_encode(<<Caustic.Utils.to_integer("0x1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")::size(256)>>, :private_key_wif, convert_from_hex: false)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58check_encode(<<Caustic.Utils.to_integer("0x1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")::size(256), 0x01>>, :private_key_wif, convert_from_hex: false)
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.base58check_encode(<<Caustic.Utils.to_integer("1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD", 16)::size(256), 0x01>>, :private_key_wif, convert_from_hex: false)
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.base58check_encode("f5f2d624cfb5c3f66d06123d0829d1c9cebf770e", :address)
"1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK"
iex> Caustic.Utils.base58check_encode("000000cb23faea20aa20f02a02955ffd1d785518", :address)
"1111DVWAb9XQh88gakJRcK14e1i1onvAL" # private key is 5KjhZsxt61XSPunjrPm8XUEAH1YN6zXm6pqT5D1hZ9mLoEAqKTp
Decodes a MIME Base64 encoded string.
https://en.wikipedia.org/wiki/Base64 (see Variants summary table)
examples
Examples
iex> Caustic.Utils.base64_decode("TWFu")
"Man"
iex> Caustic.Utils.base64_decode("TWE=")
"Ma"
iex> Caustic.Utils.base64_decode("TQ==")
"M"
iex> Caustic.Utils.base64_decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLi4u")
"Man is distinguished, not only by his reason, but by this singular passion from other animals..."
Gets the character code of a MIME base64 digit.
examples
Examples
iex> Caustic.Utils.base64_decode_char("A")
0
iex> Caustic.Utils.base64_decode_char("F")
5
iex> Caustic.Utils.base64_decode_char("a")
26
iex> Caustic.Utils.base64_decode_char("f")
31
iex> Caustic.Utils.base64_decode_char("0")
52
iex> Caustic.Utils.base64_decode_char("5")
57
iex> Caustic.Utils.base64_decode_char("+")
62
iex> Caustic.Utils.base64_decode_char("/")
63
Encodes a string into its MIME Base64 representation.
https://en.wikipedia.org/wiki/Base64 (see Variants summary table)
examples
Examples
iex> Caustic.Utils.base64_encode("Man")
"TWFu"
iex> Caustic.Utils.base64_encode("Ma")
"TWE="
iex> Caustic.Utils.base64_encode("M")
"TQ=="
iex> Caustic.Utils.base64_encode("Man is distinguished, not only by his reason, but by this singular passion from other animals...")
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLi4u"
iex> Caustic.Utils.base64_encode("Man is distinguished, not only by his reason, but by this singular passion from other animals...", new_line: false)
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLi4u"
Encodes an integer codepoint 0 <= n
<= 63 to its MIME Base64 character.
examples
Examples
iex> Caustic.Utils.base64_encode_char(0)
"A"
iex> Caustic.Utils.base64_encode_char(5)
"F"
iex> Caustic.Utils.base64_encode_char(26)
"a"
iex> Caustic.Utils.base64_encode_char(31)
"f"
iex> Caustic.Utils.base64_encode_char(52)
"0"
iex> Caustic.Utils.base64_encode_char(57)
"5"
iex> Caustic.Utils.base64_encode_char(62)
"+"
iex> Caustic.Utils.base64_encode_char(63)
"/"
Lists the exponents of n
when n
is represented as binary.
For example, 34
in binary is 100010
or 2^5 + 2^1
, so
the function will return [1, 5]
.
examples
Examples
iex> Caustic.Utils.binary_exponents(0)
[]
iex> Caustic.Utils.binary_exponents(2)
[1]
iex> Caustic.Utils.binary_exponents(32)
[5]
iex> Caustic.Utils.binary_exponents(33)
[0, 5]
Converts a Bitcoin 256-bit private key to the Wallet Import Format. Defaults to outputting compressed format.
examples
Examples
iex> Caustic.Utils.bitcoin_private_key_to_wif("1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.bitcoin_private_key_to_wif("1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", compressed: false)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
Converts a bitstring (including binary) into array of 0s and 1s. For simple binary you can also use :binary.decode_unsigned
examples
Examples
iex> Caustic.Utils.bitstring_to_array "Hey"
[0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1]
iex> Caustic.Utils.bitstring_to_array << 1 :: size(1), 0 :: size(1), 1 :: size(1) >>
[1, 0, 1]
Interprets a bitstring (including binary) as an unsigned integer. You can use :binary.decode_unsigned/1 if it's a normal binary.
examples
Examples
iex> Caustic.Utils.bitstring_to_integer(<<255, 255>>)
65535
Checks whether an integer n > 1
is a composite number. 1 is a unit,
neither a prime nor composite. Will return false on n <= 1
.
examples
Examples
iex> Caustic.Utils.composite?(4)
true
iex> Caustic.Utils.composite?(2)
false
iex> Caustic.Utils.composite?(3)
false
iex> Caustic.Utils.composite?(1)
false
Checks whether an integer is deficient.
An integer n
is abundant if and only if σ(n) - n < n
.
examples
Examples
iex> Caustic.Utils.abundant?(6)
false
Checks whether a
divides b
.
See https://math.stackexchange.com/questions/666103/why-would-some-elementary-number-theory-notes-exclude-00 and
https://math.stackexchange.com/questions/2174535/does-zero-divide-zero
examples
Examples
iex> Caustic.Utils.divides(2, 8)
true
iex> Caustic.Utils.divides(2, 3)
false
iex> Caustic.Utils.divides(2, 0)
true
iex> Caustic.Utils.divides(0, 0)
true
Gets all the positive divisors of an integer.
examples
Examples
iex> Caustic.Utils.divisors(1)
[1]
iex> Caustic.Utils.divisors(3)
[1, 3]
iex> Caustic.Utils.divisors(6)
[1, 2, 3, 6]
iex> Caustic.Utils.divisors(-4)
[1, 2, 4]
Counts how many positive divisors an integer has. d(n)
.
examples
Examples
iex> Caustic.Utils.divisors_count(1)
1
iex> Caustic.Utils.divisors_count(3)
2
iex> Caustic.Utils.divisors_count(6)
4
Sums the positive divisors of an integer. σ(n)
.
examples
Examples
iex> Caustic.Utils.divisors_sum(1)
1
iex> Caustic.Utils.divisors_sum(3)
4
iex> Caustic.Utils.divisors_sum(6)
12
The sum of the e
-th power of the positive divisors of an integer. σ_e(n)
.
For example, the divisors of 15 are 1, 3, 5, and 15, so
σ_2(15) = 1 + 3^2 + 5^2 + 15^2 = 260
examples
Examples
iex> Caustic.Utils.divisors_sum(5, 0)
2
iex> Caustic.Utils.divisors_sum(5, 1)
6
iex> Caustic.Utils.divisors_sum(5, 2)
26
iex> Caustic.Utils.divisors_sum(15, 2)
260
Find the prime factors of an integer.
examples
Examples
iex> Caustic.Utils.factorize(72)
[2, 2, 2, 3, 3]
iex> Caustic.Utils.factorize(480)
[2, 2, 2, 2, 2, 3, 5]
iex> Caustic.Utils.factorize(357171293798123)
[7, 181, 1459, 193216691]
iex> Caustic.Utils.factorize(100000001)
[17, 5882353]
iex> Caustic.Utils.factorize(9223372036854775807) # largest 64-bit integer
[7, 7, 73, 127, 337, 92737, 649657]
iex> Caustic.Utils.factorize(18446744073709551615) # largest 64-bit unsigned integer
[3, 5, 17, 257, 641, 65537, 6700417]
iex> Caustic.Utils.factorize(18446744073709551615 * 3571 * 5901331)
[3, 5, 17, 257, 641, 3571, 65537, 5901331, 6700417]
iex> Caustic.Utils.factorize(1)
[]
Find the prime factors and their exponents of an integer.
examples
Examples
iex> Caustic.Utils.factorize_grouped(9)
[{3, 2}]
iex> Caustic.Utils.factorize_grouped(72)
[{2, 3}, {3, 2}]
iex> Caustic.Utils.factorize_grouped(480)
[{2, 5}, {3, 1}, {5, 1}]
iex> Caustic.Utils.factorize_grouped(357171293798123)
[{7, 1}, {181, 1}, {1459, 1}, {193216691, 1}]
iex> Caustic.Utils.factorize_grouped(100000001)
[{17, 1}, {5882353, 1}]
iex> Caustic.Utils.factorize_grouped(9223372036854775807) # largest 64-bit integer
[{7, 2}, {73, 1}, {127, 1}, {337, 1}, {92737, 1}, {649657, 1}]
iex> Caustic.Utils.factorize_grouped(18446744073709551615) # largest 64-bit unsigned integer
[{3, 1}, {5, 1}, {17, 1}, {257, 1}, {641, 1}, {65537, 1}, {6700417, 1}]
iex> Caustic.Utils.factorize_grouped(18446744073709551615 * 3571 * 5901331)
[{3, 1}, {5, 1}, {17, 1}, {257, 1}, {641, 1}, {3571, 1}, {65537, 1}, {5901331, 1}, {6700417, 1}]
iex> Caustic.Utils.factorize(1)
[]
Finds the smallest prime divisor of a number and the result of dividing by that prime. The factorization of 1 is 1 1 and 0 is 0 1.
examples
Examples
iex> Caustic.Utils.factorize_once(20)
{2, 10}
iex> Caustic.Utils.factorize_once(11)
{11, 1}
iex> Caustic.Utils.factorize_once(1)
{1, 1}
iex> Caustic.Utils.factorize_once(0)
{0, 1}
iex> Caustic.Utils.factorize_once(-20)
{-2, 10}
Find the greatest common divisor of two integers.
examples
Examples
iex> Caustic.Utils.gcd(1, 0)
1
iex> Caustic.Utils.gcd(-1, 0)
1
iex> Caustic.Utils.gcd(270, 192)
6
iex> Caustic.Utils.gcd(-270, 192)
6
iex> Caustic.Utils.gcd(270, -192)
6
iex> Caustic.Utils.gcd(-270, -192)
6
Find the greatest common divisor d
of two integers a
and b
, while also finding
the coefficients x
and y
such that ax + by = d
.
examples
Examples
iex> Caustic.Utils.gcd_with_coefficients(3, 0)
{3, 1, 0}
iex> Caustic.Utils.gcd_with_coefficients(6, 3)
{3, 0, 1}
iex> Caustic.Utils.gcd_with_coefficients(270, 192)
{6, 5, -7}
iex> Caustic.Utils.gcd_with_coefficients(-270, 192)
{6, -5, -7}
iex> Caustic.Utils.gcd_with_coefficients(270, -192)
{6, 5, 7}
iex> Caustic.Utils.gcd_with_coefficients(-270, -192)
{6, -5, 7}
iex> Caustic.Utils.gcd_with_coefficients(314, 159)
{1, -40, 79}
Guess the base of an integer string using its prefix. Defaults to 10, and doesn't check for validity of the digits.
examples
Examples
iex> Caustic.Utils.get_base("0xabf")
16
iex> Caustic.Utils.get_base("0b101")
2
iex> Caustic.Utils.get_base("321")
10
Calculate the document hash, used for ECDSA.
Removes hexadecimal prefix from a string.
examples
Examples
iex> Caustic.Utils.hex_remove_prefix("0xff")
"ff"
iex> Caustic.Utils.hex_remove_prefix("0XFF")
"FF"
iex> Caustic.Utils.hex_remove_prefix("ff")
"ff"
Solves the linear congruence ax = b (mod m).
examples
Examples
iex> Caustic.Utils.linear_congruence_solve(1, 3, 4)
[3]
iex> Caustic.Utils.linear_congruence_solve(2, 1, 4)
[]
iex> Caustic.Utils.linear_congruence_solve(2, 6, 4)
[1, 3]
iex> Caustic.Utils.linear_congruence_solve(0, 0, 3)
[0, 1, 2]
iex> Caustic.Utils.linear_congruence_solve(0, 1, 3)
[]
iex> Caustic.Utils.linear_congruence_solve(0, 2, 3)
[]
iex> Caustic.Utils.linear_congruence_solve(0, 3, 3)
[0, 1, 2]
Splits a list into its first n
elements and the rest.
examples
Examples
iex> Caustic.Utils.list_split([1, 2, 3, 4, 5], 2)
{[1, 2], [3, 4, 5]}
iex> Caustic.Utils.list_split([1, 2, 3], 5)
{[1, 2, 3], []}
The base 2 logarithm of an integer and its remainder. For example, 9 = 2^3 + 1, so log2i(9) = 3 with remainder 1.
examples
Examples
iex> Caustic.Utils.log2i(9)
{3, 1}
iex> Caustic.Utils.log2i(8)
{3, 0}
iex> Caustic.Utils.log2i(256)
{8, 0}
Calculate the md5 hash.
https://en.wikipedia.org/wiki/Mersenne_prime
The first n
Mersenne exponents, primes p
such that 2^p - 1
is prime.
examples
Examples
iex> Caustic.Utils.mersenne_exponents_first(5)
[2, 3, 5, 7, 13]
https://en.wikipedia.org/wiki/Mersenne_prime
The i
-th Mersenne prime, starting at index 0.
Primes of the form 2^p - 1
where p
is a prime.
examples
Examples
iex> Caustic.Utils.mersenne_prime(0)
3
Finds the least residue of a number modulo m
.
examples
Examples
iex> Caustic.Utils.mod(0, 3)
0
iex> Caustic.Utils.mod(-27, 13)
12
iex> Caustic.Utils.mod(-3, 3)
0
Find the modular inverse (modular multiplicative inverse).
Using Euclidean Algorithm: https://www.math.utah.edu/~fguevara/ACCESS2013/Euclid.pdf
examples
Examples
iex> Caustic.Utils.mod_inverse(1, 101)
1
iex> Caustic.Utils.mod_inverse(2, 3)
2
iex> Caustic.Utils.mod_inverse(50, 71)
27
iex> Caustic.Utils.mod_inverse(25, 50)
nil
iex> Caustic.Utils.mod_inverse(8, 11)
7
iex> Caustic.Utils.mod_inverse(345, 76408)
48281
iex> Caustic.Utils.mod_inverse(71, 50)
31
# Bitcoin elliptic curve
iex> Caustic.Utils.mod_inverse(345, 115792089237316195423570985008687907853269984665640564039457584007908834671663)
53029420578249156164997726467746925915410601672960026429664632676085785153979
Finds the first integer n
where n >= from
satisfying some property.
The smallest number t
such that a^t = 1 (mod m)
.
Determines whether an integer is a perfect number.
A number n
is perfect iff σ(n) - n = n
.
examples
Examples
iex> Caustic.Utils.perfect?(6)
true
iex> Caustic.Utils.perfect?(5)
false
Determines whether an integer is a k-perfect number.
A number n
is k-perfect iff σ(n) = kn
.
Perfect numbers are 2-perfect.
examples
Examples
iex> Caustic.Utils.perfect?(6, 2)
true
iex> Caustic.Utils.perfect?(672, 3)
true
iex> Caustic.Utils.perfect?(2178540, 4)
true
Calculates integer exponentiation. Exponent can be negative.
examples
Examples
iex> Caustic.Utils.pow(3, 9)
19683
iex> Caustic.Utils.pow(2, 8)
256
iex> Caustic.Utils.pow(2, 256)
115792089237316195423570985008687907853269984665640564039457584007913129639936
iex> Caustic.Utils.pow(2, -2)
0.25
Fast exponentiation modulo m. Calculates x^y mod m.
With x = 5, y = 12345, m = 17, and repeated 1000 times, it is faster by naive method by a factor of 150 on a particular benchmark machine.
examples
Examples
iex> Caustic.Utils.pow_mod(5, 0, 19)
1
iex> Caustic.Utils.pow_mod(5, 1, 19)
5
iex> Caustic.Utils.pow_mod(5, 117, 19)
1
iex> Caustic.Utils.pow_mod(7, 256, 13)
9
iex> Caustic.Utils.pow_mod(2, 90, 13)
12
iex> Caustic.Utils.pow_mod(50, -1, 71)
27
iex> Caustic.Utils.pow_mod(7, -3, 13)
8
The i
-th prime, starting at index 0.
examples
Examples
iex> Caustic.Utils.prime(0)
2
iex> Caustic.Utils.prime(1)
3
iex> Caustic.Utils.prime(1999)
17389
iex> Caustic.Utils.prime(2000)
17393
iex> Caustic.Utils.prime(9999)
104729
iex> Caustic.Utils.prime(49999)
611953
iex> Caustic.Utils.prime(99999)
1299709
iex> Caustic.Utils.prime(199999)
2750159
Checks whether an integer is a prime.
examples
Examples
iex> Caustic.Utils.prime?(1)
false
iex> Caustic.Utils.prime?(2)
true
iex> Caustic.Utils.prime?(3)
true
iex> Caustic.Utils.prime?(4)
false
iex> Caustic.Utils.prime?(5882353 * 5882353)
false
Checks whether an integer is a prime using Sieve of Eratosthenes algorithm. Don't use on very large numbers.
Find all primes p ≤ n
using Sieve of Eratosthenes method.
examples
Examples
iex> Caustic.Utils.prime_sieve_up_to(10)
[2, 3, 5, 7]
iex> Caustic.Utils.prime_sieve_up_to(30)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
iex> Caustic.Utils.prime_sieve_up_to(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
The first n
primes.
Checks whether a number n
is a primitive root modulo m
.
examples
Examples
iex> Caustic.Utils.primitive_root?(1, 5)
false
iex> Caustic.Utils.primitive_root?(4, 5)
false
iex> Caustic.Utils.primitive_root?(2, 5)
true
Finds the primitive roots of a positive integer m
.
a
is a primitive root of an integer m
if a
is a least residue
and has order (mod m
) of t = φ(n)
.
This means that t
is the smallest number such that a^t = 1 (mod m)
.
Gets all the positive divisors of a number, excluding the number itself.
examples
Examples
iex> Caustic.Utils.proper_divisors(10)
[1, 2, 5]
Counts how many proper divisors an integer has. d(n) - 1
.
examples
Examples
iex> Caustic.Utils.proper_divisors_count(1)
0
iex> Caustic.Utils.proper_divisors_count(3)
1
iex> Caustic.Utils.proper_divisors_count(6)
3
Removes the first element of a list.
examples
Examples
iex> Caustic.Utils.select([[:a, :b], [1, 2, 3]])
[[:a, 1], [:a, 2], [:a, 3], [:b, 1], [:b, 2], [:b, 3]]
iex> Caustic.Utils.select([[["hello"], ["bye"]], ["world"]])
[[["hello"], "world"], [["bye"], "world"]]
examples
Examples
iex> Caustic.Utils.smallest_prime_divisor(2)
2
iex> Caustic.Utils.smallest_prime_divisor(4)
2
iex> Caustic.Utils.smallest_prime_divisor(144)
2
iex> Caustic.Utils.smallest_prime_divisor(5)
5
iex> Caustic.Utils.smallest_prime_divisor(35)
5
If n
is a square number, return its root. Otherwise nil
.
examples
Examples
iex> Caustic.Utils.sqrti(1)
1
iex> Caustic.Utils.sqrti(4)
2
iex> Caustic.Utils.sqrti(5)
nil
iex> Caustic.Utils.sqrti(-1)
nil
Creates a lazy stream of integers satisfying some property.
Finds the index of an ASCII character inside a string. Not unicode friendly!
examples
Examples
iex> Caustic.Utils.string_index_of("Hello", "H")
0
iex> Caustic.Utils.string_index_of("Hello", "h")
nil
iex> Caustic.Utils.string_index_of("Hello", "l")
2
Finds all subsets of a set (represented by a keyword list).
examples
Examples
iex> Caustic.Utils.subsets([:a])
[[], [:a]]
iex> Caustic.Utils.subsets([:a, :b, :c])
[[], [:a], [:b], [:c], [:a, :b], [:a, :c], [:b, :c], [:a, :b, :c]]
Finds all subsets with cardinality n
of a set (represented by a keyword list).
examples
Examples
iex> members = ["nakai", "kusanagi", "mori", "kimura", "katori", "inagaki"]
iex> Caustic.Utils.subsets(members, 3)
[
["nakai", "kusanagi", "mori"],
["nakai", "kusanagi", "kimura"],
["nakai", "kusanagi", "katori"],
["nakai", "kusanagi", "inagaki"],
["nakai", "mori", "kimura"],
["nakai", "mori", "katori"],
["nakai", "mori", "inagaki"],
["nakai", "kimura", "katori"],
["nakai", "kimura", "inagaki"],
["nakai", "katori", "inagaki"],
["kusanagi", "mori", "kimura"],
["kusanagi", "mori", "katori"],
["kusanagi", "mori", "inagaki"],
["kusanagi", "kimura", "katori"],
["kusanagi", "kimura", "inagaki"],
["kusanagi", "katori", "inagaki"],
["mori", "kimura", "katori"],
["mori", "kimura", "inagaki"],
["mori", "katori", "inagaki"],
["kimura", "katori", "inagaki"]
]
Determines whether an integer is a superperfect number.
A number n
is superperfect if and only if σ(σ(n)) = 2n
.
examples
Examples
iex> Caustic.Utils.superperfect?(2)
true
iex> Caustic.Utils.superperfect?(4)
true
iex> Caustic.Utils.superperfect?(16)
true
iex> Caustic.Utils.superperfect?(3)
false
Find the digits of a nonnegative integer n
in a particular base
.
examples
Examples
iex> Caustic.Utils.to_digits(321, 10)
[3, 2, 1]
iex> Caustic.Utils.to_digits(5, 2)
[1, 0, 1]
iex> Caustic.Utils.to_digits(255, 16)
[15, 15]
iex> Caustic.Utils.to_digits(0, 8)
[0]
Parse a string which can be in any base to integer, autodetecting the base using the prefix.
examples
Examples
iex> Caustic.Utils.to_integer("0xff")
255
iex> Caustic.Utils.to_integer("0b101")
5
iex> Caustic.Utils.to_integer("321")
321
Parse a string which can be in any base to integer, specifying the base.
examples
Examples
iex> Caustic.Utils.to_integer("ff", 16)
255
iex> Caustic.Utils.to_integer("101", 2)
5
iex> Caustic.Utils.to_integer("755", 8)
493
iex> Caustic.Utils.to_integer("321", 10)
321
Convert an integer into its string representation in any base
.
examples
Examples
iex> Caustic.Utils.to_string(255, 16)
"0xff"
iex> Caustic.Utils.to_string(255, 16, prefix: false)
"ff"
iex> Caustic.Utils.to_string(5, 2)
"0b101"
If an integer can be written as the sum of the squares of two positive integers, return those two integers {a, b} where a <= b.
examples
Examples
iex> Caustic.Utils.to_sum_of_two_squares(17)
{1, 4}
iex> Caustic.Utils.to_sum_of_two_squares(1)
nil
Euler's totient function φ(n)
.
The number of positive integers less than or equal to m
and relatively prime to m
.
examples
Examples
iex> Caustic.Utils.totient(5)
4
iex> Caustic.Utils.totient(6)
2
iex> Caustic.Utils.totient(9)
6
iex> Caustic.Utils.totient(1)
1
iex> Caustic.Utils.totient(4200)
960
Positive integers less than or equal to m
and relatively prime to m
.
examples
Examples
iex> Caustic.Utils.totient_members(1)
[1]
iex> Caustic.Utils.totient_members(6)
[1, 5]
iex> Caustic.Utils.totient_members(9)
[1, 2, 4, 5, 7, 8]
iex> Caustic.Utils.totient_members(10)
[1, 3, 7, 9]
Checks whether an integer is triangular.
An integer is triangular if and only if it can be expressed as n(n + 1)/2
for some integer n
.
examples
Examples
iex> Caustic.Utils.triangular? 0
true
iex> Caustic.Utils.triangular? 3
true
iex> Caustic.Utils.triangular? 4
false
Finds the nonnegative integer m
such that m(m + 1)/2 = n
.
Returns nil
if no such number exists.
examples
Examples
iex> Caustic.Utils.triangular_root 6
3