如何找到在研发连续的合数 [英] how to find consecutive composite numbers in R

查看:241
本文介绍了如何找到在研发连续的合数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先'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 

这篇关于如何找到在研发连续的合数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆