swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: 39aca747f500f69985711f7ae5e9969f93ecc860 authored by Hans W. Borchers on 21 June 2017, 07:52:27 UTC
version 2.0.7
version 2.0.7
Tip revision: 39aca74
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]
}