## check if a given integer is prime isPrime = function(n) { if (n <= 3) { return (TRUE) } if (any((n %% 2:floor(sqrt(n))) == 0)) { return (FALSE) } return (TRUE) } ## estimate mean only using observation with prime indices estMeanPrimes = function (x) { n = length(x) ind = sapply(1:n, isPrime) return (mean(x[ind])) } print(estMeanPrimes(rnorm(100000)))