# `NumberF.Calculations`
[🔗](https://github.com/jamesnjovu/elixir_number_functions/blob/v0.3.0/lib/number_f/calculations.ex#L1)

General numeric calculation functions.

# `combinations`

Calculates combinations (n choose k) — the binomial coefficient.

Returns an `t: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

# `digit_sum`

Sum of the decimal digits.

## Examples

    iex> NumberF.Calculations.digit_sum(12345)
    15

# `digital_root`

Repeatedly sums the digits until one digit remains.

## Examples

    iex> NumberF.Calculations.digital_root(12345)
    6

# `digits`

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]

# `divisors`

All positive divisors of n, ascending.

## Examples

    iex> NumberF.Calculations.divisors(28)
    [1, 2, 4, 7, 14, 28]

# `factorial`

Calculates factorial of a non-negative integer.

# `fibonacci`

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

# `fibonacci_sequence`

The first n Fibonacci numbers.

## Examples

    iex> NumberF.Calculations.fibonacci_sequence(8)
    [0, 1, 1, 2, 3, 5, 8, 13]

# `from_base`

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

# `from_binary`

Parses a binary integer.

## Examples

    iex> NumberF.Calculations.from_binary("101")
    5

# `from_hex`

Parses a hexadecimal integer.

## Examples

    iex> NumberF.Calculations.from_hex("FF")
    255

# `from_octal`

Parses an octal integer.

## Examples

    iex> NumberF.Calculations.from_octal("10")
    8

# `gcd`

Calculates the Greatest Common Divisor (GCD) of two integers.

# `in_range?`

Checks if a number is within a specified range (inclusive).

# `interpolate`

Performs a linear interpolation between two points.

# `is_prime?`

Checks if a number is prime.

# `lcm`

Calculates the Least Common Multiple (LCM) of two integers.

# `log_base`

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

# `nth_root`

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

# `palindrome?`

Whether the decimal representation reads the same in both directions.

## Examples

    iex> NumberF.Calculations.palindrome?(12321)
    true

    iex> NumberF.Calculations.palindrome?(12345)
    false

# `percentage`

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

# `perfect_square?`

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

# `permutations`

Number of ordered arrangements of k items from n (nPk).

## Examples

    iex> NumberF.Calculations.permutations(5, 2)
    20

# `prime_factors`

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]

# `reverse_number`

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

# `round_to_nearest`

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

# `to_base`

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"

# `to_binary`

Renders an integer in binary.

## Examples

    iex> NumberF.Calculations.to_binary(5)
    "101"

# `to_degrees`

Converts radians to degrees.

# `to_hex`

Renders an integer in hexadecimal.

## Examples

    iex> NumberF.Calculations.to_hex(255)
    "FF"

# `to_octal`

Renders an integer in octal.

## Examples

    iex> NumberF.Calculations.to_octal(8)
    "10"

# `to_radians`

Converts degrees to radians.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
