简体   繁体   English

求R中给定n的k个光滑数

[英]Find k-smooth numbers for a given n in R

Need to find all the k-smooth numbers for a given n. 需要找到给定n的所有k光滑数。 I tried the following: 我尝试了以下方法:

library(gmp)

S <- c(1:30)

test <- function(range, k){
  if(!isprime(k)==2){
    print("k should be a prime number")
  }
  else{
    for(i in range[1]:range[length(range)]){
      pf <- as.integer(factorize(i))
      if(max(pf) <= k){ 
        print(range[i]) 
      }
    }

  }
}

At the console: test(s, 11) 在控制台上:测试(s,11)

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 14
[1] 15
[1] 16
[1] 18
[1] 20
[1] 21
[1] 22
[1] 24
[1] 25
[1] 27
[1] 28
[1] 30


 *Warning message:
    In max(pf) : no non-missing arguments to max; returning -Inf*

I want to get rid of the warning. 我想摆脱警告。 Please help! 请帮忙! Is it a problem with the factorize() function returning bigz. factorize()函数返回bigz是否有问题? changed it to as.integer(factorize(i)) ? 将其更改为as.integer(factorize(i))吗? Not able to understand the reason for the warning 无法理解警告的原因

The problem is 问题是

factorize(1)
# bigz(0)
length(factorize(1))
# [1] 0

That returns an empty vector. 返回一个空向量。 And when you take the max of an empty vector, you get that warning 当您取空向量的最大值时,您会得到警告

max(numeric())
# [1] -Inf
# Warning message:
# In max(numeric()) : no non-missing arguments to max; returning -Inf

And you get there because 而你到达那里是因为

isprime(1)
# 0

so by the definition of isprime() 1 is not a prime number. 所以根据isprime()的定义1不是素数。 So i'm not sure how you want to handle the number 1, but that is your problem. 所以我不确定您要如何处理数字1,但这就是您的问题。

The problem is with the first value in S. 问题出在S中的第一个值。

as.integer(factorize(1)) produces integer(0) and max(integer(0)) produces that error. as.integer(factorize(1))产生integer(0)max(integer(0))产生该错误。

You can fix it like so: 您可以这样修复它:

if (length(pf)!=0) {
   if(max(pf) <= k){
      print(range[i]) 
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM