sequences v1.2.1 Sequences

The Sequences module defines multiple methods that return a Stream of numbers, usually integers.

The different Streams can be tapped in on-demand, by running any Enum function on them. Be warned: Do not use any function that iterates through the complete Stream. If you try this, your code will hang, as Elixir will never finish iterating through the infinite lists.

For efficiency, these sequences are calculated in a way that re-uses previously calculated results whenever possible.

See https://github.com/Qqwy/elixir-sequences for more information.

Link to this section Summary

Functions

An infinite Stream containing the Catalan numbers (A000108).

An ascending Stream containing the even integers (A005843)

An infinite Stream containing the Factorial numbers (A000142).

An infinite Stream containing the Fibonacci numbers (A000045).

An ascending Stream containing the nonnegative integers (A001477)

Defines an infinitely continuing integer Stream, starting at start , with step step between values. step defaults to 1.

An ascending Stream containing the odd integers (A005408)

An infinite Stream of ones (A000012)

Returns an infinite stream of Pell-Lucas numbers.

Returns an infinite stream of Pell numbers.

Returns an infinite stream of Rational numbers, combining both the Pell numbers and the Pell-Lucas numbers.

Returns an infinite stream of tuples, combining both the Pell numbers and the Pell-Lucas numbers.

Returns a stream of digits of the mathematical constant π (pi) including the starting 3, using Gibbons' Spigot algorithm.

An ascending Stream containing the positive integers (A000027)

Defines an ascending integer Stream, containing the Prime numbers (A000040).

Returns a Stream of values 1-9, representing the decimal expansion of the square root of n.

Returns a Stream of values 1-9, representing the decimal expansion of the square root of n.

Returns a tuple where the first value is the integer square root of n, and the second value is a Stream that contains the decimal expansion of the square root of n.

Returns a tuple where the first value is the integer square root of n, and the second value is a List that contains amount_of_digits digits of the decimal expansion of the square root of n.

An infinite Stream containing the Triangular numbers (A000217).

An infinite Stream of zeroes (A000004)

Link to this section Functions

An infinite Stream containing the Catalan numbers (A000108).

Runs in O(n²), memory consumption is O(n²)

Definition:

  • C(0) = 1
  • C(1) = 1
  • C(n) = Σ( C(i) * C(n-i)) for all i <- 0 <= i < n

Examples:

iex> Sequences.catalan |> Enum.take(5)
[1, 2, 5, 14, 42]
Link to this function

even_integers()

An ascending Stream containing the even integers (A005843)

Examples:

iex> Sequences.even_integers |> Enum.take(5)
[0,2,4,6,8]

An infinite Stream containing the Factorial numbers (A000142).

Runs in O(n)

Definition:

  • fact(0) = 1
  • fact(n) = n * fact(n-1)

Examples:

iex> Sequences.factorials |> Enum.take(5)
[1, 1, 2, 6, 24]

An infinite Stream containing the Fibonacci numbers (A000045).

Runs in O(n)

Definition:

  • fib(0) = 1
  • fib(1) = 1
  • fib(n) = fib(n-1) + fib(n-2)

Examples:

iex> Sequences.fibonacchi |> Enum.take(5)
[1, 1, 2, 3, 5]

An ascending Stream containing the nonnegative integers (A001477)

Examples:

iex> Sequences.integers |> Enum.take(5)
[0,1,2,3,4]
Link to this function

integers(start, step \\ 1)

Defines an infinitely continuing integer Stream, starting at start , with step step between values. step defaults to 1.

Usage:

Sequences.integers(start, step)

Examples:

iex> Sequences.integers(0,3) |> Enum.take(5)
[0,3,6,9,12]

iex> Sequences.integers(10,-1) |> Enum.take(5)
[10,9,8,7,6]

An ascending Stream containing the odd integers (A005408)

Examples:

iex> Sequences.odd_integers |> Enum.take(5)
[1,3,5,7,9]

An infinite Stream of ones (A000012)

Examples:

iex> Sequences.ones |> Enum.take(5)
[1,1,1,1,1]
Link to this function

pell_lucas_numbers()

Returns an infinite stream of Pell-Lucas numbers.

Pell-Lucas numbers are the numerators of the closest rational approximations to the square root of 2.

Returns an infinite stream of Pell numbers.

Pell numbers are the denominators of the closest rational approximations to the square root of 2.

Link to this function

pell_rationals()

Returns an infinite stream of Rational numbers, combining both the Pell numbers and the Pell-Lucas numbers.

These Rational numbers form the approximations to the square root of 2. The Rationals are constructed using the Ratio library.

Examples:

iex> Enum.take Sequences.pell_rationals, 10
[1, 3 <|> 2, 7 <|> 5, 17 <|> 12, 41 <|> 29, 99 <|> 70, 239 <|> 169, 577 <|> 408,
1393 <|> 985, 3363 <|> 2378]

Returns an infinite stream of tuples, combining both the Pell numbers and the Pell-Lucas numbers.

These tuples form the closest rational approximations to the square root of 2.

Returns a stream of digits of the mathematical constant π (pi) including the starting 3, using Gibbons' Spigot algorithm.

This algorithm(see http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf) is less fast than dedicated algorithms with a specified and fixed upper bound, but it has the advantage that you can specify exactly how many digits you need at a later time.

Examples:

iex> Sequences.pi |> Enum.take(20)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4]
Link to this function

positive_integers()

An ascending Stream containing the positive integers (A000027)

Examples:

iex> Sequences.positive_integers |> Enum.take(5)
[1,2,3,4,5]

Defines an ascending integer Stream, containing the Prime numbers (A000040).

This function uses Sequences.Primes.trial_division internally, although this might change in the future when more, faster prime-discovery methods are added.

Runs in O(n*sqrt(n)/ln(n)²)

Examples:

iex> Sequences.primes |> Enum.take(10)
[2,3,5,7,11,13,17,19,23,29]
Link to this function

squareroot_decimals(n)

Returns a Stream of values 1-9, representing the decimal expansion of the square root of n.

The decimal expansion is calculated using the Square Roots By Extraction technique described here.

Examples

iex> Sequences.squareroot_decimals(2) |> Enum.take(20)
[4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 0, 4, 8, 8, 0]
iex> Sequences.squareroot_decimals(100) |> Enum.take(20)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Link to this function

squareroot_expansion(n)

Returns a Stream of values 1-9, representing the decimal expansion of the square root of n.

Note that squareroot_expansion/1 does not strip away the integral part at the front. Use squareroot_decimals/1 for that.

The decimal expansion is calculated using the Square Roots By Extraction technique described here.

Examples

iex> Sequences.squareroot_expansion(2) |> Enum.take(20)
[1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 0, 4, 8, 8]
iex> Sequences.squareroot_expansion(100) |> Enum.take(20)
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Link to this function

squareroot_tuple(n)

Returns a tuple where the first value is the integer square root of n, and the second value is a Stream that contains the decimal expansion of the square root of n.

The decimal expansion is calculated using the Square Roots By Extraction technique described here.

Examples

iex> {a, b} = Sequences.squareroot_tuple(42); {a, Enum.take(b, 10)}
{6, [4, 8, 0, 7, 4, 0, 6, 9, 8, 4]}
Link to this function

squareroot_tuple(n, amount_of_digits)

Returns a tuple where the first value is the integer square root of n, and the second value is a List that contains amount_of_digits digits of the decimal expansion of the square root of n.

The decimal expansion is calculated using the Square Roots By Extraction technique described here.

Examples

iex> Sequences.squareroot_tuple(100, 3)
{10, [0, 0, 0]}
iex> Sequences.squareroot_tuple(2, 10)
{1, [4, 1, 4, 2, 1, 3, 5, 6, 2, 3]}
iex> Sequences.squareroot_tuple(82, 10)
{9, [0, 5, 5, 3, 8, 5, 1, 3, 8, 1]}

An infinite Stream containing the Triangular numbers (A000217).

Definition

  • L(0) = 0
  • L(1) = 1
  • L(n) = L(n-2)+(2*n)-1

Examples:

iex> Sequences.triangular |> Enum.take(5)
[0, 1, 3, 6, 10]

An infinite Stream of zeroes (A000004)

Examples:

iex> Sequences.zeroes |> Enum.take(5)
[0,0,0,0,0]