Learn R Programming

DescTools (version 0.99.56)

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 integers to be quite fast.

Usage

Primes(n)

Value

numeric vector of all prime numbers \(\le n\).

Arguments

n

a (typically positive integer) number.

Author

Bill Venables (\(\le n\)); Martin Maechler gained another 40% speed, working with logicals and integers.

Details

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

See Also

Factorize, GCD, LCM, IsPrime

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