| 1 |
\name{primes} |
| 2 |
\alias{primes} |
| 3 |
\title{Find all Primes Less Than n} |
| 4 |
\description{ |
| 5 |
Find all prime numbers aka \sQuote{primes} less than \eqn{n}. |
| 6 |
|
| 7 |
Uses an obvious sieve method (and some care), working with |
| 8 |
\code{\link{logical}} and and \code{\link{integer}}s to be quite fast. |
| 9 |
} |
| 10 |
\usage{ |
| 11 |
primes(n, pSeq = NULL) |
| 12 |
} |
| 13 |
\arguments{ |
| 14 |
\item{n}{a (typically positive integer) number.} |
| 15 |
\item{pSeq}{optionally a vector of primes (2,3,5,...) as if from a |
| 16 |
\code{primes()} call; \bold{must} be correct. |
| 17 |
The goal is a speedup, but currently we have not found one single |
| 18 |
case, where using a non-NULL \code{pSeq} is faster.} |
| 19 |
} |
| 20 |
\details{ |
| 21 |
As the function only uses \code{\link{max}(n)}, \code{n} can also be a |
| 22 |
\emph{vector} of numbers. |
| 23 |
|
| 24 |
The famous prime number theorem states that \eqn{\pi(n)}, the |
| 25 |
\emph{number} of primes below \eqn{n} is asymptotically \eqn{n / |
| 26 |
\log(n)} in the sense that \eqn{\lim_{n \to \infty}{\pi(n) \cdot \log(n) / |
| 27 |
n \sim 1}}{lim[n -> Inf] \pi(n) * log(n) / n ~ 1}. |
| 28 |
|
| 29 |
Equivalently, the inverse of \eqn{pi()}, the \eqn{n}-th prime number |
| 30 |
\eqn{p_n} is around \eqn{n \log n}; recent results (Pierre Dusart, 1999), |
| 31 |
prove that |
| 32 |
\deqn{\log n + \log\log n - 1 < \frac{p_n}{n} < \log n + \log \log n |
| 33 |
\quad\mathrm{for } n \ge 6.}{% |
| 34 |
log n + log log n - 1 < p_n / n < log n + log log n for n >= 6.} |
| 35 |
} |
| 36 |
\value{ |
| 37 |
numeric vector of all prime numbers \eqn{\le n}{<= n}. |
| 38 |
} |
| 39 |
\author{Bill Venables (<= 2001); Martin Maechler gained another 40\% speed, |
| 40 |
carefully working with logicals and integers. |
| 41 |
} |
| 42 |
\seealso{ |
| 43 |
\code{\link{factorize}}. For large \eqn{n}, use the \CRANpkg{gmp} package |
| 44 |
and its \code{\link[gmp]{isprime}} and \code{\link[gmp]{nextprime}} |
| 45 |
functions. |
| 46 |
} |
| 47 |
\examples{ |
| 48 |
(p1 <- primes(100)) |
| 49 |
system.time(p1k <- primes(1000)) # still lightning fast |
| 50 |
stopifnot(length(p1k) == 168) |
| 51 |
\donttest{ |
| 52 |
system.time(p.e7 <- primes(1e7)) # still only 0.3 sec (2015 (i7)) |
| 53 |
stopifnot(length(p.e7) == 664579) |
| 54 |
## The famous pi(n) := number of primes <= n: |
| 55 |
pi.n <- approxfun(p.e7, seq_along(p.e7), method = "constant") |
| 56 |
pi.n(c(10, 100, 1000)) # 4 25 168 |
| 57 |
plot(pi.n, 2, 1e7, n = 1024, log="xy", axes = FALSE, |
| 58 |
xlab = "n", ylab = quote(pi(n)), |
| 59 |
main = quote("The prime number function " ~ pi(n))) |
| 60 |
eaxis(1); eaxis(2) |
| 61 |
} |
| 62 |
|
| 63 |
## Exploring p(n) := the n-th prime number ~=~ n * pnn(n), where |
| 64 |
## pnn(n) := log n + log log n |
| 65 |
pnn <- function(n) { L <- log(n); L + log(L) } |
| 66 |
n <- 6:(N <- length(PR <- primes(1e5))) |
| 67 |
m.pn <- cbind(l.pn = ceiling(n*(pnn(n)-1)), pn = PR[n], u.pn = floor(n*pnn(n))) |
| 68 |
matplot(n, m.pn, type="l", ylab = quote(p[n]), main = quote(p[n] ~~ |
| 69 |
"with lower/upper bounds" ~ n*(log(n) + log(log(n)) -(1~"or"~0)))) |
| 70 |
## (difference to the lower approximation) / n --> ~ 0.0426 (?) : |
| 71 |
plot(n, PR[n]/n - (pnn(n)-1), type = 'l', cex = 1/8, log="x", xaxt="n") |
| 72 |
eaxis(1); abline(h=0, col=adjustcolor(1, 0.5)) |
| 73 |
} |
| 74 |
\keyword{math} |
| 75 |
\keyword{arithmetic} |