Caustic v0.1.19 Caustic.Utils View Source

A collection of useful methods.

Link to this section Summary

Functions

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

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 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

Find the prime factors of an integer

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

Calculate the md5 hash

Finds the least residue of a number modulo m

Find the modular inverse

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

Gets all the positive divisors of a number

Counts how many positive divisors an integer has. d(n)

Sums the positive divisors of an integer. σ(n)

Calculates integer exponentiation. Exponent can be negative

Fast exponentiation modulo m. Calculates x^y mod m

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

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)

Examples

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)

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

Link to this section Functions

Link to this function base58_decode(str, opts \\ []) View Source

Decode a base58-encoded string into its hexadecimal string representation.

Examples

iex> Caustic.Utils.base58_decode("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn")
"0x801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff"
iex> Caustic.Utils.base58_decode("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn", prefix: false)
"801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff"
Link to this function base58_encode(str, opts \\ []) View Source

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

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

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}
Link to this function base58check_encode(payload, version, opts \\ []) View Source

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

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

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

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
Link to this function base64_encode(data, opts \\ []) View Source

Encodes a string into its MIME Base64 representation.

https://en.wikipedia.org/wiki/Base64 (see Variants summary table)

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

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)
"/"
Link to this function bitcoin_private_key_to_wif(hex_str, opts \\ []) View Source

Converts a Bitcoin 256-bit private key to the Wallet Import Format. Defaults to outputting compressed format.

Examples

iex> Caustic.Utils.bitcoin_private_key_to_wif("1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.bitcoin_private_key_to_wif("1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", compressed: false)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
Link to this function bitstring_to_array(data) View Source

Converts a bitstring (including binary) into array of 0s and 1s. For simple binary you can also use :binary.decode_unsigned

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]
Link to this function bitstring_to_integer(data) View Source

Interprets a bitstring (including binary) as an unsigned integer. You can use :binary.decode_unsigned/1 if it’s a normal binary.

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

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 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

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
Link to this function exponentiation_table_mod(m) View Source
Link to this function exponentiation_table_mod(m, rows, cols) View Source
Link to this function exponentiation_table_mod_print(m, opts \\ [relatively_prime_only: false]) View Source

Find the prime factors of an integer.

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 greatest common divisor of two integers.

Proof: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm

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
Link to this function gcd_with_coefficients(a, b) View Source

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

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

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

iex> Caustic.Utils.hex_remove_prefix("0xff")
"ff"
iex> Caustic.Utils.hex_remove_prefix("0XFF")
"FF"
iex> Caustic.Utils.hex_remove_prefix("ff")
"ff"
Link to this function linear_congruence_solve(a, b, m) View Source

Solves the linear congruence ax = b (mod m).

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]
Link to this function linear_congruence_solve_explain(a, b, m) View Source

Splits a list into its first n elements and the rest.

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], []}

Calculate the md5 hash.

Link to this function mersenne_primes_first(n) View Source
Link to this function mersenne_primes_first_25() View Source

Finds the least residue of a number modulo m.

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.

Using Euclidean Algorithm: https://www.math.utah.edu/~fguevara/ACCESS2013/Euclid.pdf

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
Link to this function multiplication_table_mod(m) View Source
Link to this function multiplication_table_mod_print(m) View Source
Link to this function order_multiplicative(n, m) View Source

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

Gets all the positive divisors of a number.

Examples

iex> Caustic.Utils.positive_divisors(1)
[1]
iex> Caustic.Utils.positive_divisors(3)
[1, 3]
iex> Caustic.Utils.positive_divisors(6)
[1, 2, 3, 6]
iex> Caustic.Utils.positive_divisors(-4)
[1, 2, 4]
Link to this function positive_divisors_count(n) View Source

Counts how many positive divisors an integer has. d(n).

Examples

iex> Caustic.Utils.positive_divisors_count(1)
1
iex> Caustic.Utils.positive_divisors_count(3)
2
iex> Caustic.Utils.positive_divisors_count(6)
4
Link to this function positive_divisors_sum(n) View Source

Sums the positive divisors of an integer. σ(n).

Examples

iex> Caustic.Utils.positive_divisors_sum(1)
1
iex> Caustic.Utils.positive_divisors_sum(3)
4
iex> Caustic.Utils.positive_divisors_sum(6)
12

Calculates integer exponentiation. Exponent can be negative.

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.

https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/modular-exponentiation https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation

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

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

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]

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).

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"]]
Link to this function smallest_prime_divisor(n) View Source

Finds the index of an ASCII character inside a string. Not unicode friendly!

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

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

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"]
]

Find the digits of a nonnegative integer n in a particular base.

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

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

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
Link to this function to_string(n, base, opts \\ []) View Source

Convert an integer into its string representation in any base.

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"
Link to this function to_sum_of_two_squares(i) View Source

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

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

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

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]
Link to this function write_to_file(list, path, item_per_line) View Source