How to find sequential compound numbers in R

I want the first 'n' consecutive compound numbers

I was looking for a command to search for sequential compound numbers, but I got a result confirming this with a torus. I have not received any team for this. Please help me solve this problem in R.

-3
source share
3 answers

Here is another option:

n_composite <- function(n) {

  s <- 4L
  i <- 1L
  vec <- numeric(n)
  while(i <= n) {

    if(any(s %% 2:(s-1) == 0L)) {
      vec[i] <- s
      i <- i + 1L

    }

    s <- s + 1L
  }

  vec
}

It uses basic control flows to loop through positive integer indices.

test

all.equal(find_N_composites(1e4), n_composite(1e4))
[1] TRUE

library(microbenchmark)
microbenchmark(

  Mak = find_N_composites(1e4),
  plafort = n_composite(1e4),
  times=5

  )

Unit: milliseconds
    expr       min        lq      mean    median        uq
     Mak 2304.8671 2347.9768 2397.0620 2376.4306 2475.2368
 plafort  508.8132  509.3055  522.1436  509.3608  530.4311
       max neval cld
 2480.7988     5   b
  552.8076     5  a 
+2
source

The @Pierre Lafortune code is neat and not too slow, but I would suggest a different approach, which is significantly faster.

, n R " n+k ". , 1:(n+k) , , numbers::Primes().

n+k, n (k1) , . , (n+1):(n+k1) k2 , . , , ... .

- : , (non-prime) , . : () ( , , ). , , .

, , :

library(numbers)

n_composite2 <- function(n, from = 2) {

  endRange <- from + n - 1
  numbers <- seq(from = from, to = endRange)
  primes <- Primes(n1 = from, n2 = endRange)
  composites <- numbers[!(numbers %in% primes)]
  nPrimes <- length(primes)
  if (nPrimes >= 1) return(c(composites, n_composite2(nPrimes, from = endRange + 1)))

  return(composites)
}

( ), numbers::Primes() . , , [number of primes in previous step] , .

, , (n_composite()):

> all(n_composite(1e4) == n_composite2(1e4))
[1] TRUE

, n_composite2() 19 :

library(microbenchmark)
microbenchmark(
  "n_composite2" = n_composite2(1e4),
  "n_composite" = n_composite(1e4),
  times=5
)

Unit: milliseconds
         expr       min        lq      mean    median        uq       max neval
 n_composite2  34.44039  34.51352  35.10659  34.71281  35.21145  36.65476     5
  n_composite 642.34106 661.15725 666.02819 662.99657 671.52093 692.12512     5

: "" " . numbers::Primes() while, , n_composite(). " " , n . , , ( ).

+1

, ; :

is_composite<-function(x){
            sapply(x,function(y) if(y<3){FALSE}else{any(y%%(2:(y-1))==0)})
}
which(is_composite(1:100))

find_N_composites<-function(N){
    which(is_composite(1:(2*N+2)))[1:N]
}
find_N_composites(10)

system.time({
        x<-find_N_composites(1e+04)
})

The idea is, therefore, to check every number if it has any divisors other than 1 and itself. The function I provided detects the first 10,000 compound numbers in about 2 seconds. If you want to increase speed in large quantities, it is better to optimize it. For example, if you look for divisors only among primes.

0
source

Source: https://habr.com/ru/post/1606768/


All Articles