如何找到在研发连续的合数合数

由网友(渣女°)分享简介:我想先'N'连续的合数I want first 'n' consecutive composite numbers我搜索命令查找连续的合数,但我得到的结果证明该thorem。我没有得到任何命令that..please帮我在研发解决售后服务这个问题。I searched command for finding co...

我想先'N'连续的合数

I want first 'n' consecutive composite numbers

我搜索命令查找连续的合数,但我得到的结果证明该thorem。我没有得到任何命令that..please帮我在研发解决售后服务这个问题。

I searched command for finding consecutive composite numbers, but i got the result proving for that thorem. I didn't get any command for that..please help me to slove this problem in R.

推荐答案

下面是另一种选择:

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 cycle through positive integers indexing composites.

基准

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 
阅读全文

相关推荐

最新文章