Skip to content
A prime number library for Elixir, bindings around the Primal Rust library
Elixir Rust
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
lib
native/primalex_nativeprime
test
.gitignore
LICENSE
README.md
mix.exs
mix.lock

README.md

PrimalEx

NIF Elixir bindings for the Primal Rust library.

It utilizes the Rustler library to handle the NIF interop. Be aware of the caveats outlined in this blog post.

NOTE: version 0.2.0 and later make use of Erlang's dirty schedulers, which are supported by default in OPT 20 and later.

Installation

The package can be installed by adding prime_ex to your list of dependencies in mix.exs:

def deps do
  [{:primal_ex, "~> 0.2.0"}]
end

You will need Rust installed as well. Official installation instructions are here

Usage examples

# Get a list of primes less than 40
PrimalEx.primes(40)
>>> {:ok, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]}

# Get a list of the primes between 100 and 120
PrimalEx.primes(100, 120)
>>> {:ok, [101, 103, 107, 109, 113]}

# Get a list of the first 10 primes
PrimalEx.n_primes(10)
>>> {:ok, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]}

# Get a list of the first 10 primes >= 1000
PrimalEx.n_primes(10, 1000)
>>> {:ok, [1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061]}

# Get the 10th prime
PrimalEx.nth_prime(10)
>>> {:ok, 29}

# Count the primes below 10**9
PrimalEx.count_primes(10**9)
>>> {:ok, 50847534}

You can’t perform that action at this time.