The function of the right number in R

I am trying to create a function to check 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 does not work, although I cannot understand why. I check if a given number can be divisible by any of the integers up to this number without a remainder. If this is not possible, then the number is prime.

Another solution I found:

 tpn <- function(pn){ if(sum(pn/1:pn==pn%/%1:pn)==2) print("prime") } 

It works. Although, I can’t understand that sum(pn/1:pn == pn%/%1:pn) == 2 really tests.

+10
r primes
source share
11 answers

The number a is divided by the number b if the division result a / b is equal to the result of integer division a %/% b . Any integer pn can be divided into at least two numbers: 1 and pn . The right numbers are those that can only be separated by these two. Code output:

  • pn / 1:pn - results of divisions by 1 , 2 , ..., pn
  • pn %/% 1:pn - results of integer divisions by 1 , 2 , ..., pn
  • sum(pn / 1:pn == pn %/% 1:pn) - how many of them are equal, i.e. the number of integer divisors pn . If this number is 2 , you have a prime.

What's wrong with your code: if you need to check if there is something TRUE or FALSE , but you passed the whole vector to it. Also, your logic was wrong. It should have been:

 is.prime <- function(num) { if (num == 2) { TRUE } else if (any(num %% 2:(num-1) == 0)) { FALSE } else { TRUE } } 

And once you decide to go back to logical, you can make the code much shorter:

 is.prime <- function(n) n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0) 

(which includes @Carl's comment on not checking all numbers.)

+20
source share

I just tried the is.prime code is.prime . But with this 3 is not easy, o)

The improved version uses a ceiling instead of a ceiling.

 is.prime <- function(n) n == 2L || all(n %% 2L:ceiling(sqrt(n)) != 0) 

Best!

+7
source share

You can also use the isprime() function in the matlab package. It also works with vector arguments:

 library(matlab) as.logical(isprime(7)) as.logical(isprime(42)) #> as.logical(isprime(7)) #[1] TRUE #> as.logical(isprime(42)) #[1] FALSE 
+4
source share

Regular expression for finding prime numbers

 is.prime <- function(x) { x <- abs(as.integer(x)) !grepl('^1?$|^(11+?)\\1+$', strrep('1', x)) } (-100:100)[is.prime(-100:100)] # [1] -97 -89 -83 -79 -73 -71 -67 -61 -59 -53 -47 -43 -41 -37 -31 -29 -23 -19 -17 -13 -11 -7 -5 -3 -2 # [26] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

http://diswww.mit.edu/bloom-picayune.mit.edu/perl/10138


Or, if you take all integers from 1 to x , the number that is divisible without a remainder should be 2: 1 and x

 is.prime <- function(x) vapply(x, function(y) sum(y / 1:y == y %/% 1:y), integer(1L)) == 2L (1:100)[is.prime(1:100)] # [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

I knew regex would be the slowest, but it's still my favorite

 is.prime <- function(x) vapply(x, function(y) sum(y / 1:y == y %/% 1:y), integer(1L)) == 2L is.prime_regex <- function(x) { x <- abs(as.integer(x)) !grepl('^1?$|^(11+?)\\1+$', strrep('1', x)) } is.prime_Seily <- function(n) vapply(n, function(y) y == 2L || all(y %% 2L:ceiling(sqrt(y)) != 0), logical(1L)) is.prime_flodel <- function(n) vapply(n, function(y) y == 2L || all(y %% 2L:max(2,floor(sqrt(y))) != 0), logical(1L)) x <- 1:1000 library('microbenchmark') microbenchmark( is.prime(x), is.prime_regex(x), is.prime_Seily(x), is.prime_flodel(x), unit = 'relative' ) # Unit: relative # expr min lq mean median uq max neval cld # is.prime(x) 8.593971 8.606353 8.805690 8.892905 9.724452 21.9886734 100 b # is.prime_regex(x) 84.572928 86.200415 76.413036 86.895956 85.117796 25.7106323 100 c # is.prime_Seily(x) 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000 100 a # is.prime_flodel(x) 1.146212 1.147971 1.144839 1.146119 1.163302 0.9085948 100 a 
+3
source share

I am going to provide you 2 simple functions. The second shows the nth prime. EDIT * (typo)

 PrimeNumber <- function(n){ #Eratosthenes #Return all prime numbers up to n (based on the sieve of Eratosthenes) if (n >= 2) { sieve <- seq(2, n) primes <- c() for (i in seq(2, n)) { if (any(sieve == i)) { primes <- c(primes, i) sieve <- c(sieve[(sieve %% i) != 0], i) } } return(primes) } else { stop("Input value of n should be at least 2.") } } testScript <- function(n){ i=3 v=c(2) while (length(v)<=n-1){ if (all((i%%v[v<ceiling(sqrt(i))])!=0)){ v=c(v,i) } i=i+2; } return(v) } 
+1
source share

Here is another way to find a prime using a simple concept.

 is.prime <- function(n){ if (n == 2){ # finds the square root of number and assign to var 'i' print('number is prime') } else if (n > 2){ i <- sqrt(n) # finds the square root of number and assign to var 'i' i <- round(i, digits = 1) # if square root generates decimals, round it to one place vec <- c(2:i) #creating vector to load numbers from 2 to 'i' d <- n %% (vec) #dividing each number generated by vector by the input number 'n' if ( 0 %in% d){ # check to see if any of the result of division is 0 print('number is not prime') #if any of the result of division is 0, number is not prime } else{ print('number is prime') } } } is.prime(2) [1] "number is prime" is.prime(131) #calling the function with the desired number [1] "number is prime" is.prime(237) [1] "number is not prime" 
+1
source share

This is a vectorized version with an additional check of the natural number:

 is.prime <- Vectorize(function(n) ifelse(round(n) == n, n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0), NA)); #> is.prime(c(1:10, 1.1)) # [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE NA 
0
source share
  is.primeornot <- function(n) { count = 0 for( i in 2:n) { if(n%%i == 0){count = count+1} } p = c(n) if(count > 1){ print(paste(n,"is not a prime number"))} else{print("prime")} } 
0
source share

Here is the most compact code I think:

 is_prime <- function(n){ ifelse(sum(n %% (1:n) == 0) > 2, FALSE, TRUE) } 

If you need to check whether each element of a vector of numbers is a prime number, you can do:

 is_prime2 <- Vectorize(FUN = is_prime, vectorize.args = "n") 

Now is_prime2() works with vectors.

0
source share

prime or not:

prime.or.not <-function (x) {ifelse (0% in% (x %% (2: (x-1))), "not simple", "simple")}

0
source share

1) Any number n can have only one prime factor greater than sqrt (n) 2) All primes greater than 3 can be written as 6k + / -1

  is.prime <- function(n){ x1<- (if (n==1){return(FALSE)} else if(n<4){return(TRUE)} else if(n%%2==0){return(FALSE)} else if(n<9){return(TRUE)} else if(n%%3==0){return(FALSE)} else { r=floor(n^.5) f=5 while(f<=r){ if(n%%f==0){return(FALSE);break} else if(n%%(f+2)==0){return(FALSE);break} f=f+6 } }) if(is.null(x1)){return(TRUE)} } 
0
source share

All Articles