General numeric calculation functions.
Summary
Functions
Calculates combinations (n choose k) — the binomial coefficient.
Sum of the decimal digits.
Repeatedly sums the digits until one digit remains.
The decimal digits of an integer, most significant first.
All positive divisors of n, ascending.
Calculates factorial of a non-negative integer.
The nth Fibonacci number, counting from fibonacci(0) == 0.
The first n Fibonacci numbers.
Parses an integer written in an arbitrary base from 2 to 36.
Parses a binary integer.
Parses a hexadecimal integer.
Parses an octal integer.
Calculates the Greatest Common Divisor (GCD) of two integers.
Checks if a number is within a specified range (inclusive).
Performs a linear interpolation between two points.
Checks if a number is prime.
Calculates the Least Common Multiple (LCM) of two integers.
Logarithm of a number in an arbitrary base.
The real nth root of a number.
Whether the decimal representation reads the same in both directions.
Calculates a percentage with specified precision.
Whether n is a perfect square.
Number of ordered arrangements of k items from n (nPk).
Prime factorisation, in ascending order and with repeats.
The integer with its digits reversed. The sign is preserved.
Rounds a number to the nearest specified value.
Renders an integer in an arbitrary base from 2 to 36.
Renders an integer in binary.
Converts radians to degrees.
Renders an integer in hexadecimal.
Renders an integer in octal.
Converts degrees to radians.
Functions
Calculates combinations (n choose k) — the binomial coefficient.
Returns an integer/0. A count of ways to choose things is never fractional,
and the previous factorial(n) / (factorial(k) * factorial(n - k)) returned a
float, so combinations(30, 15) came back as 155117520.0 and anything past
roughly n = 60 silently lost precision to the float conversion.
Examples
iex> NumberF.Calculations.combinations(5, 2)
10
iex> NumberF.Calculations.combinations(30, 15)
155_117_520
Sum of the decimal digits.
Examples
iex> NumberF.Calculations.digit_sum(12345)
15
Repeatedly sums the digits until one digit remains.
Examples
iex> NumberF.Calculations.digital_root(12345)
6
The decimal digits of an integer, most significant first.
Examples
iex> NumberF.Calculations.digits(12345)
[1, 2, 3, 4, 5]
iex> NumberF.Calculations.digits(-42)
[4, 2]
All positive divisors of n, ascending.
Examples
iex> NumberF.Calculations.divisors(28)
[1, 2, 4, 7, 14, 28]
Calculates factorial of a non-negative integer.
The nth Fibonacci number, counting from fibonacci(0) == 0.
Precision
Iterative, so it is linear rather than exponential and stays exact for any n —
Erlang integers are arbitrary precision, so fibonacci(1000) is the true value
rather than a float approximation.
Examples
iex> NumberF.Calculations.fibonacci(10)
55
iex> NumberF.Calculations.fibonacci(0)
0
The first n Fibonacci numbers.
Examples
iex> NumberF.Calculations.fibonacci_sequence(8)
[0, 1, 1, 2, 3, 5, 8, 13]
Parses an integer written in an arbitrary base from 2 to 36.
Examples
iex> NumberF.Calculations.from_base("FF", 16)
255
iex> NumberF.Calculations.from_base("11111111", 2)
255
Parses a binary integer.
Examples
iex> NumberF.Calculations.from_binary("101")
5
Parses a hexadecimal integer.
Examples
iex> NumberF.Calculations.from_hex("FF")
255
Parses an octal integer.
Examples
iex> NumberF.Calculations.from_octal("10")
8
Calculates the Greatest Common Divisor (GCD) of two integers.
Checks if a number is within a specified range (inclusive).
Performs a linear interpolation between two points.
Checks if a number is prime.
Calculates the Least Common Multiple (LCM) of two integers.
Logarithm of a number in an arbitrary base.
Examples
iex> NumberF.Calculations.log_base(1024, 2)
10.0
iex> NumberF.Calculations.log_base(1000, 10)
3.0
The real nth root of a number.
Examples
iex> NumberF.Calculations.nth_root(27, 3)
3.0
iex> NumberF.Calculations.nth_root(16, 4)
2.0
Whether the decimal representation reads the same in both directions.
Examples
iex> NumberF.Calculations.palindrome?(12321)
true
iex> NumberF.Calculations.palindrome?(12345)
false
Calculates a percentage with specified precision.
Edge cases
A zero total raises ArgumentError. Letting the division run would surface an
ArithmeticError from inside this function, which tells the caller nothing about
which argument was wrong.
Examples
iex> NumberF.Calculations.percentage(25, 200, 2)
12.5
Whether n is a perfect square.
Precision
Compares integers rather than testing sqrt(n) == trunc(sqrt(n)), which starts
reporting false negatives once n exceeds the float mantissa.
Examples
iex> NumberF.Calculations.perfect_square?(144)
true
iex> NumberF.Calculations.perfect_square?(145)
false
Number of ordered arrangements of k items from n (nPk).
Examples
iex> NumberF.Calculations.permutations(5, 2)
20
Prime factorisation, in ascending order and with repeats.
Examples
iex> NumberF.Calculations.prime_factors(360)
[2, 2, 2, 3, 3, 5]
iex> NumberF.Calculations.prime_factors(17)
[17]
The integer with its digits reversed. The sign is preserved.
Examples
iex> NumberF.Calculations.reverse_number(12345)
54321
iex> NumberF.Calculations.reverse_number(-120)
-21
Rounds a number to the nearest specified value.
Always returns a float, matching NumberF.round_to_nearest/2. The two copies of
this function had drifted — this one returned an integer for integer input while
the facade multiplied by 1.0 — so callers got a different type depending on which
module they reached for.
Examples
iex> NumberF.Calculations.round_to_nearest(12, 5)
10.0
iex> NumberF.Calculations.round_to_nearest(127, 5)
125.0
Renders an integer in an arbitrary base from 2 to 36.
Examples
iex> NumberF.Calculations.to_base(255, 16)
"FF"
iex> NumberF.Calculations.to_base(255, 2)
"11111111"
Renders an integer in binary.
Examples
iex> NumberF.Calculations.to_binary(5)
"101"
Converts radians to degrees.
Renders an integer in hexadecimal.
Examples
iex> NumberF.Calculations.to_hex(255)
"FF"
Renders an integer in octal.
Examples
iex> NumberF.Calculations.to_octal(8)
"10"
Converts degrees to radians.