swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: 162b33221cccd2760e0dd44598539c521ffcd73b authored by HwB on 18 March 2011, 00:00:00 UTC
version 0.2-2
version 0.2-2
Tip revision: 162b332
primes.R
###
### PRIMES.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]
}