sequences v0.0.2 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.

Summary

Functions

An infinite Stream containing the Catalan numbers (A000108)

An ascending Stream containing the even integers (A005843)

Example

An infinite Stream containing the Factorial numbers (A000142)

An infinite Stream containing the Fibonacci numbers (A000045)

An ascending Stream containing the nonnegative integers (A001477)

Example

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)

Example

An infinite Stream of ones (A000012)

Example

An ascending Stream containing the positive integers (A000027)

Example

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

An infinite Stream containing the Triangular numbers (A000217)

An infinite Stream of zeroes (A000004)

Functions

catalan()

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

Example:

iex> Sequences.catalan |> Enum.take(5)
[1, 2, 5, 14, 42]
even_integers()

An ascending Stream containing the even integers (A005843)

Example:

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

An infinite Stream containing the Factorial numbers (A000142).

Runs in O(n)

Definition:

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

Example:

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

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)

Example:

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

An ascending Stream containing the nonnegative integers (A001477)

Example:

iex> Sequences.integers |> Enum.take(5)
[0,1,2,3,4]
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)

Example:

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]
odd_integers()

An ascending Stream containing the odd integers (A005408)

Example:

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

An infinite Stream of ones (A000012)

Example:

iex> Sequences.ones |> Enum.take(5)
[1,1,1,1,1]
positive_integers()

An ascending Stream containing the positive integers (A000027)

Example:

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

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)²)

Example:

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

An infinite Stream containing the Triangular numbers (A000217).

Definition

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

Example:

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

An infinite Stream of zeroes (A000004)

Example:

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