Q:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
R
1st version:
to <- 2000000
nums <- 1:to
nums[1] <- -1
index <- 1
while(index<sqrt(to)){
index <- which.max(nums>0)
if(is.null(index)){
break
}
nums[index] <- -nums[index]
nums[seq(2*index,to,by=index)] <- 0
}
nums[which(nums<0)] <- -nums[which(nums<0)]
sum(nums)