Learn R Programming

sfsmisc (version 1.0-15)

primes: Find all Primes Less Than n

Description

Find all prime numbers aka primes less than $n$.

Uses an obvious sieve method (and some care, working with logical and and integers to be quite fast.

Usage

primes(n)

Arguments

n
a (typically positive integer) number.

Value

  • numeric vector of all prime numbers $\le n$.

Details

As the function only uses max(n), n can also be a vector of numbers.

See Also

factorize; next in base R's (stats package).

Examples

Run this code
(p1 <- primes(100))
 system.time(p1k <- primes(1000)) # still lightning ..
 stopifnot(length(p1k) == 168)

Run the code above in your browser using DataLab