Integer (Elixir v1.20.5)

Copy Markdown View Source

Functions for working with integers.

Some functions that work on integers are found in Kernel:

Summary

Guards

Determines if an integer is even.

Determines if integer is odd.

Functions

Performs a ceiled integer division.

Returns the ordered digits for the given integer.

Returns the extended greatest common divisor of the two given integers.

Performs a floored integer division.

Returns the greatest common divisor of the two given integers.

Computes the modulo remainder of an integer division.

Parses a text representation of an integer.

Counts the number of set bits (1) in the binary representation of a non-negative integer.

Computes base raised to power of exponent.

Returns a charlist which corresponds to the text representation of integer in the given base.

Returns a binary which corresponds to the text representation of integer in the given base.

Returns the integer represented by the ordered digits.

Guards

is_even(integer)

(macro)

Determines if an integer is even.

Returns true if the given integer is an even number, otherwise it returns false.

Allowed in guard clauses.

Examples

iex> Integer.is_even(10)
true

iex> Integer.is_even(5)
false

iex> Integer.is_even(-10)
true

iex> Integer.is_even(0)
true

is_odd(integer)

(macro)

Determines if integer is odd.

Returns true if the given integer is an odd number, otherwise it returns false.

Allowed in guard clauses.

Examples

iex> Integer.is_odd(5)
true

iex> Integer.is_odd(6)
false

iex> Integer.is_odd(-5)
true

iex> Integer.is_odd(0)
false

Functions

ceil_div(dividend, divisor)

(since 1.20.0)
@spec ceil_div(integer(), neg_integer() | pos_integer()) :: integer()

Performs a ceiled integer division.

Raises an ArithmeticError exception if one of the arguments is not an integer, or when the divisor is 0.

This function performs a ceiled integer division, which means that the result will always be rounded towards positive infinity.

Examples

iex> Integer.ceil_div(5, 2)
3
iex> Integer.ceil_div(6, -4)
-1
iex> Integer.ceil_div(-99, 2)
-49

digits(integer, base \\ 10)

@spec digits(integer(), pos_integer()) :: [integer(), ...]

Returns the ordered digits for the given integer.

An optional base value may be provided representing the radix for the returned digits. This one must be an integer >= 2.

Examples

iex> Integer.digits(123)
[1, 2, 3]

iex> Integer.digits(170, 2)
[1, 0, 1, 0, 1, 0, 1, 0]

iex> Integer.digits(-170, 2)
[-1, 0, -1, 0, -1, 0, -1, 0]

extended_gcd(a, b)

(since 1.12.0)
@spec extended_gcd(integer(), integer()) :: {non_neg_integer(), integer(), integer()}

Returns the extended greatest common divisor of the two given integers.

This function uses the extended Euclidean algorithm to return a three-element tuple with the gcd and the coefficients m and n of Bézout's identity such that:

gcd(a, b) = m*a + n*b

By convention, extended_gcd(0, 0) returns {0, 0, 0}.

Examples

iex> Integer.extended_gcd(240, 46)
{2, -9, 47}
iex> Integer.extended_gcd(46, 240)
{2, 47, -9}
iex> Integer.extended_gcd(-46, 240)
{2, -47, -9}
iex> Integer.extended_gcd(-46, -240)
{2, -47, 9}

iex> Integer.extended_gcd(14, 21)
{7, -1, 1}

iex> Integer.extended_gcd(10,