View Source Multitool.Numbers.Sequence (multitool v0.3.4)

Provides operations that generate sequences of numbers

Link to this section Summary

Functions

Provides the Collatz sequence starting with the given number and ending at 1

Given the integer n, returns the length of the resulting Collatz sequence, including the starting integer

Returns a Stream that supplies the Fibonacci sequence, starting at 1

Returns a list of the first n integers in the Fibonacci sequence

Returns the integer at the nth position in the Fibonacci sequence, where the first integer in the sequence is 1

Link to this section Functions

Link to this function

collatz(n)

View Source (since 1.3.0)

Provides the Collatz sequence starting with the given number and ending at 1

Returns an empty list if a number less than 1 is provided

parameters

Parameters

n: Positive integer to begin the sequence at

examples

Examples

iex> collatz(2)
[2, 1]

iex> collatz(0)
[]

iex> collatz(3)
[3, 10, 5, 16, 8, 4, 2, 1]
Link to this function

collatz_len(n)

View Source (since 1.3.0)

Given the integer n, returns the length of the resulting Collatz sequence, including the starting integer

Returns zero when the provided integer is less than one. This method is optimized for calculating the length over taking the length of the resulting list of collatz/1

parameters

Parameters

n: Positive integer

examples

Examples

iex> collatz_len(2)
2

iex> collatz_len(0)
0

iex> collatz_len(3)
8
Link to this function

fibonacci()

View Source (since 1.3.0)

Returns a Stream that supplies the Fibonacci sequence, starting at 1

examples

Examples

iex> fibonacci() |> Enum.take(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]  
Link to this function

fibonacci(n)

View Source (since 1.3.0)

Returns a list of the first n integers in the Fibonacci sequence

An empty list is returned if a number less than 1 is provided

parameters

Parameters

n: An integer, the number of digits in the sequence to retrieve

examples

Examples

iex> fibonacci(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]  
Link to this function

nth_fibonacci(n)

View Source (since 1.3.0)

Returns the integer at the nth position in the Fibonacci sequence, where the first integer in the sequence is 1

When an integer less than zero is provided, nil is returned. When zero is provided, zero is returned. All other values are computed

parameters

Parameters

n: An integer, the nth digit to retrieve from the Fibonacci sequence

examples

Examples

iex> nth_fibonacci(10)
55 

iex> nth_fibonacci(-1)
nil

iex> nth_fibonacci(0)
0