Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Counting primes using the Sieve of Eratosthenes in R
# The Sieve of Eratosthenes
# Given a number greater than zero this function will return a list of primes between 2 and the number given as argument.
sieveOfEratosthenes <- function(num){
values <- rep(TRUE, num)
values[1] <- FALSE <- 2
for(i in{
values[ *, num,] <- FALSE <- + min(which(values[( + 1) : num]))
length(sieveOfEratosthenes(2000)) # 303
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment