首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写孪生素数函数

编写孪生素数函数
EN

Stack Overflow用户
提问于 2016-10-20 02:05:57
回答 2查看 1.9K关注 0票数 0

我们知道孪生素数是一对素数(x,y),因此y=x+ 2。我想要创建一个函数Thatit(),它可以列出n下面的所有孪生素数。如果知道函数Eratosthenes()列出了n的素数(即介于2到n之间),我如何修改我的代码以得到这样的函数?

代码语言:javascript
复制
    Thatit <- function(n){
      twin <- c()
      twin2 <- Eratosthenes(n)
    for(i in twin2){
      if((i+2) in twin2){
        twin <- c(twin, "(", (i, i+2), ")")
      } else next()
    } return(twin)
}
EN

回答 2

Stack Overflow用户

发布于 2016-10-20 02:59:58

我们可以避免for循环,并有:

代码语言:javascript
复制
TwinPrimesLong = function(n) {

if (n < 5) stop("Input value of n should be at least 5.")

primesLessThanN = Eratosthenes(n)

#Combine differenced sets with original prime set
twinPrimeSet = cbind( (primesLessThanN-2) ,primesLessThanN)

#Keep only those rows where differenced numbers are also primes i.e. belong to prime set
twinPrimeSet = data.frame(matrix(twinPrimeSet[(primesLessThanN-2) %in% primesLessThanN,],ncol=2))

colnames(twinPrimeSet)=c("TwinPrime1","TwinPrime2")

cat("The twin primes less than n = ",n,"in row form are :\n")

cat(paste0("(",twinPrimeSet[,1],",",twinPrimeSet[,2],")",collapse=","),"\n")

cat("\n\nThe twin primes less than n = ",n,"in table form are :\n")

print(twinPrimeSet)

#If you wish to return,uncomment below
# return(twinPrimeSet)

}

更新:

使用diff,它可以进一步简化为

(primesLessThanN[diff(primesLessThanN)==2],primesLessThanN[diff(primesLessThanN)==2]+2)

代码语言:javascript
复制
TwinPrimesShort = function(n) {

if (n < 5) stop("Input value of n should be at least 5.")

primesLessThanN = Eratosthenes(n)


#Use diff function to compute difference with previous value and check if == 2
TwinPrime1 = primesLessThanN[diff(primesLessThanN)==2]
TwinPrime2 = TwinPrime1 + 2

#Keep only those rows where differenced numbers are also primes i.e. belong to prime set 
twinPrimeSet = data.frame(TwinPrime1=TwinPrime1,TwinPrime2=TwinPrime2)

cat("The twin primes less than n = ",n,"in row form are :\n")

cat(paste0("(",twinPrimeSet[,1],",",twinPrimeSet[,2],")",collapse=","),"\n")

cat("\n\nThe twin primes less than n = ",n,"in table form are :\n")

print(twinPrimeSet)

}

输出:

代码语言:javascript
复制
TwinPrimesLong(100)
#The twin primes less than n =  100 in row form are :
#(3,5),(5,7),(11,13),(17,19),(29,31),(41,43),(59,61),(71,73) 
#
#
#The twin primes less than n =  100 in table form are :
#  TwinPrime1 TwinPrime2
#1          3          5
#2          5          7
#3         11         13
#4         17         19
#5         29         31
#6         41         43
#7         59         61
#8         71         73
票数 1
EN

Stack Overflow用户

发布于 2016-10-20 12:54:43

除对(3,5)外,所有孪生素数对某些k的形式为6k±1,因此,要找到小于n的孪生素数,首先计算筛分素数小于sqrt(n),初始化长度n/4的位数组,使所有值都为真,然后再筛两次。在第一次筛选中,当k= 0,1,2时,位数组的指数代表6k+1.。。因此,对于每个筛分质数,找出最小的6k+1,这是从这一点开始的筛分素和筛子的倍数。在第二次筛选中,当k= 0,1,2时,位数组的索引为6k+1.。。因此,对于每个筛分质数,找出最小的6k+1,这是从这一点开始的筛分素和筛子的倍数。在这两个筛选中幸存的位数组位置表示孪生素数;您可以从索引中计算出两个孪生素数。这个算法非常快,它只是Eratosthenes的筛子。我在我的博客讨论了该算法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40144293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档