Need to find all the k-smooth numbers for a given n. I tried the following:
需要找到给定n的所有k平滑数。我尝试了以下方法:
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)
在控制台:test(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. changed it to as.integer(factorize(i)) ? Not able to understand the reason for the warning
我想摆脱警告。请帮忙!是factorize()函数返回bigz的问题。把它改为as.integer(factorize(i))?无法理解警告的原因
2 个解决方案
#1
0
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))产生整数(0)和max(整数(0))产生该错误。
You can fix it like so:
您可以像这样修复它:
if (length(pf)!=0) {
if(max(pf) <= k){
print(range[i])
}
}
#2
1
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. So i'm not sure how you want to handle the number 1, but that is your problem.
所以通过isprime()的定义,1不是素数。所以我不确定你想如何处理数字1,但那是你的问题。
#1
0
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))产生整数(0)和max(整数(0))产生该错误。
You can fix it like so:
您可以像这样修复它:
if (length(pf)!=0) {
if(max(pf) <= k){
print(range[i])
}
}
#2
1
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. So i'm not sure how you want to handle the number 1, but that is your problem.
所以通过isprime()的定义,1不是素数。所以我不确定你想如何处理数字1,但那是你的问题。