swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: 7e2814c76ec9bc4503af661775ea73f11ec16537 authored by Hans W. Borchers on 15 November 2014, 00:00:00 UTC
version 1.7.9
version 1.7.9
Tip revision: 7e2814c
primes.R
###
### p r i m e s . R Prime numbers
###
primes <- function(n) {
if (!is.numeric(n) || length(n) != 1)
stop("Argument 'n' must be a numeric scalar.")
n <- floor(n)
if (n < 2) return(c())
p <- seq(1, n, by=2)
q <- length(p)
p[1] <- 2
if (n >= 9) {
for (k in seq(3, sqrt(n), by=2)) {
if (p[(k+1)/2] != 0)
p[seq((k*k+1)/2, q, by=k)] <- 0
}
}
p[p > 0]
}