primes {sfsmisc} | R Documentation |
Find all prime numbers aka ‘primes’ less than n.
Uses an obvious sieve method (and some care, working with
logical
and and integer
s to be quite fast.
primes(n)
n |
a (typically positive integer) number. |
As the function only uses max(n)
, n
can also be a
vector of numbers.
numeric vector of all prime numbers <= n.
Bill Venables (<= 2001); Martin Maechler gained another 40% speed, working with logicals and integers.
factorize
; next
in “base” R's
(stats package).
(p1 <- primes(100)) system.time(p1k <- primes(1000)) # still lightning .. stopifnot(length(p1k) == 168)