I wanted to write a program which will print and count all the primes between 1 and k. The idea is that we take each odd number i between 1 and k and check for odd factors of i up to sqrt(i). It took my computer 1 minute 47 seconds to go check for primes up to k = 1 000 000.
Then I tried to improve my code: instead of checking all odd numbers from 1 to k, I removed all the multiples of 3, 5, and 7 from these odd numbers, so basically I removed about half the numbers we have to check. I ran the program again, and with k = 1 000 000 I got a time of 1 minute and 40 seconds, only a 7 second improvement.
Here is my code:
#The program has some weird quirks
#(it doesn't print 3 and 5 because of some errors I was getting) but other than that
#it seems to work fine
#k is the number that we check up to
k <- 1000000
#n is the number of primes, initialized at 2
n <- 2
#We take only the odd numbers between 5 and k
y <- seq(5, k, 2)
#We take each member of i and check it for primality
for (i in y) {
#i is assumed to be prime until proved otherwise
prime <- TRUE
#We check the remainder when i is divided by every odd number less than its square root
for (j in c(2, seq(3, ceiling(sqrt(i)), 2))) {
if (i %% j == 0) {
#If it's found that some number divides i, we set prime to false and move on
#to the next i
prime <- FALSE
break
}
}
#If no number has divided i, we haven't broken the loop so R will get to this point
#We shouldn't need the if statement, but for some reason the n counter
#gets too big if I remove the statement
if (prime) {
print(i)
n <- n + 1
}
}
#Print out the number of primes
print(paste("There are", n, "prime numbers between", 1, "and", k))
In the version where I also remove multiples of 3, 5, and 7, I just define y as
y <- setdiff((setdiff((setdiff(seq(5, k, 2), seq(6, k, 3))), seq(10, k, 5))), seq(14, k, 7))
Sorry it's very messy, but it works.