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

Provides operations for working with prime numbers

A number is prime if it has only two factors, itself and one

Link to this section Summary

Functions

Returns the next prime number to follow c

Returns the nth prime number, where the first prime number is two.

Checks if the given number is a prime number.

Generates a Stream of prime numbers, starting at the first prime number, two.

Generates a list of n prime numbers, starting at the first prime number, two.

Returns a Stream that returns prime numbers that occur after the given number

Returns a list of n primes that occur after, and not including, c

Returns a list of prime numbers that occur before the given integer n

Returns a list of n primes that occur before, and not including, c

Link to this section Functions

Link to this function

next_prime(c)

View Source (since 1.3.0)

Returns the next prime number to follow c

parameters

Parameters

c: Integer. The number to get the next prime after

examples

Examples

iex> next_prime(0)
2

iex> next_prime(59651)
59659 
Link to this function

nth_prime(n)

View Source (since 0.1.0)

Returns the nth prime number, where the first prime number is two.

Returns nil when n is less than one

parameters

Parameters

n: The nth prime number to retrieve

examples

Examples

iex> nth_prime(3)
5
iex> nth_prime(-1)
nil
iex> nth_prime(98764)
1282201

Checks if the given number is a prime number.

Returns true if the number is prime, false otherwise. Numbers less than two always return false

parameters

Parameters

n: Integer to check for primality

examples

Examples

iex> prime?(-1)
false

iex> prime?(1) 
false

iex> prime?(3)
true

iex> prime?(9800)
false

Generates a Stream of prime numbers, starting at the first prime number, two.

This operation will produce prime numbers until a terminating condition is reached

examples

Examples

iex> primes() |> Enum.take(0) 
[]

iex> primes() |> Enum.take(1)
[2]

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

Generates a list of n prime numbers, starting at the first prime number, two.

An empty list is returned when n is less than one

parameters

Parameters

n: The number of prime numbers to generate

examples

Examples

iex> primes(-1)
[]

iex> primes(1)
[2]

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

primes_after(n)

View Source (since 1.3.0)

Returns a Stream that returns prime numbers that occur after the given number

This operation will produce prime numbers until a terminating condition is reached

parameters

Parameters

 n: An integer to generate primes after

examples

Examples

iex> primes_after(2) |> Enum.take(1)
[3]

iex> primes_after(0) |> Enum.take(2)
[2, 3]
Link to this function

primes_after(c, n)

View Source (since 1.3.0)

Returns a list of n primes that occur after, and not including, c

parameters

Parameters

c: Integer. The number to generate primes after
n: Integer. The number of primes to generate

examples

Examples

iex> primes_after(2, 1)
[3]

iex> primes_after(0, 2)
[2, 3]
Link to this function

primes_before(n)

View Source (since 1.3.0)

Returns a list of prime numbers that occur before the given integer n

An empty list is returned when n is less than three

parameters

Parameters

n: An integer to retrieve preceding primes of

examples

Examples

iex> primes_before(3)
[2]

iex> primes_before(2)
[]

iex> primes_before(10)
[7, 5, 3, 2]
Link to this function

primes_before(c, n)

View Source (since 1.3.0)

Returns a list of n primes that occur before, and not including, c

parameters

Parameters

c: Integer. The number to generate primes before
n: Integer. The number of primes to generate

examples

Examples

iex> primes_before(5, 2)
[3, 2]

iex> primes_before(2)
[]