I want to write a function in R which accepts a list of integers and returns only the values which are prime.
So far I have this:
primefindlist<-function(n){
return(n[n==2 | all(n %% seq(2,ceiling(sqrt(n)),by=1) !=0)])
}
But I keep getting an error message when I run the function e.g;
primefindlist(c(7,11))
Error in seq.default(2, ceiling(sqrt(n)), by = 1) : 'to' must be of length 1
Anyone got any ideas how to overcome this?
Also the code below tells me if a single integer is prime or not ie is.prime(7) outputs TRUE
is.prime <- function(n) n == 2L || all(n %% 2L:ceiling(sqrt(n)) != 0)