Learn R Programming

Fast Functions for Prime Numbers in R

Authors: Os Keyes and Paul Egeler
License: MIT

Description

This R package has several utility functions for dealing with prime numbers, such as checking for primality and generating 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
  • finding prime k-tuples (e.g., twin primes)
  • finding the greatest common divisor and smallest (least) common multiple
  • testing whether two numbers are coprime
  • computing Euler's totient (phi)

The package also provides an R dataset containing the first one thousand primes.

Installation

You can install the released version of primes from CRAN with:

install.packages("primes")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("ironholds/primes")

Example

This checks which of the first twenty natural numbers are prime:

library(primes)

is_prime(1:20)
## [1] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
## [15] FALSE FALSE  TRUE FALSE  TRUE FALSE

You can also generate all the prime numbers between 101 and 199 with the following:

generate_primes(101, 199)
## [1] 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199

Copy Link

Version

Install

install.packages('primes')

Monthly Downloads

472

Version

1.6.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

January 9th, 2024

Functions in primes (1.6.0)

k_tuple

Prime k-tuples
prime_factors

Perform Prime Factorization on a Vector
gcd

Find the Greatest Common Divisor, Smallest Common Multiple, or Coprimality
is_prime

Test for Prime Numbers
nth_prime

Get the n-th Prime from the Sequence of Primes.
next_prime

Find the Next and Previous Prime Numbers
prime_count

Prime-counting Functions and Estimating the Value of the n-th Prime
phi

Euler's Totient Function
primes

Pre-computed Prime Numbers
primorial

Compute the Primorial
ruth_aaron_pairs

Find Ruth-Aaron Pairs of Integers
generate_n_primes

Generate a Sequence of Prime Numbers