Prime number function in R
问题 I am trying to create a function to test if a given integer is a prime number, I tried using the following: tpn <- function(prime.num){ if(prime.num==2){ print("PRIME") } else { if(prime.num%%(2:(prime.num-1))!=0){ print("PRIME") } else { print("NOT PRIME") }}} This doesn't work, although I cant understand why. I am checking to see if the given number can be divided by any of the integers up to this number with no remainders. If it cant, then the number is prime. Another solution I found was: