Fast Functions for Prime Numbers

Fast functions for dealing with prime numbers, such as testing whether a number is prime and generating a sequence prime numbers. Additional functions include finding prime factors and Ruth-Aaron pairs, finding next and previous prime numbers in the series, finding or estimating the nth prime, estimating the number of primes less than or equal to an arbitrary number, computing primorials, prime k-tuples (e.g., twin primes), finding the greatest common divisor and smallest (least) common multiple, testing whether two numbers are coprime, and computing Euler's totient function. Most functions are vectorized for speed and convenience.

Author: Oliver Keyes
License: MIT
Status: In development


primes is a simple package that does two things; tests for prime numbers, and generates prime numbers. Testing can be done with is_prime, which accepts an integer vector of any length and returns a logical vector of equal length containing, for each element, whether it is prime (TRUE) or not (FALSE). generate_primes generates every prime number between min and max, where min is 0 by default.

Both functions rely on a C++ implementation of Wilson's theorem for testing primality; in theory this is a very slow test, but in practice, due to the restrictions R places around maximum integer sizes, it's fast enough for our needs.


For the development version:


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


1.1.0 by Os Keyes, 8 months ago

Report a bug at

Browse source code at

Authors: Os Keyes [aut, cre] , Paul Egeler [aut]

Documentation:   PDF Manual  

Task views: Numerical Mathematics

MIT + file LICENSE license

Imports Rcpp

Suggests testthat

Linking to Rcpp

System requirements: C++11

Imported by sfaR.

Suggested by EGAnet.

See at CRAN