【问题标题】:How can I use the gmp package to utilise a loop for an extremely large number?如何使用 gmp 包将循环用于非常大的数字?
【发布时间】:2020-08-06 15:07:02
【问题描述】:

我开发了一个循环来计算给定数字的最大素数。

for(i in (p-1):(1)){
if ((p%%i)==0){
p<-append(p,i)
}
}

n<-c()
for (i in p){
if (sum(i/1:i==i%/%1:i)==2){
n=c(n,i)
}
}

max<-max(n)

这适用于 r 可以处理的数字。但是,当我为大数字 600851475143 尝试此操作时,我收到错误:

long vectors not supported yet

我知道这是 r 无法处理大量数字的问题。我已经尝试通过使用 gmp 包来解决这个问题:

install.packages("gmp")
library(gmp)

并将 as.bigz 与我的号码一起使用:

 p<-as.bigz(600851475143)

但这会返回错误:

Error in (p - 1):(1) : unimplemented type 'raw' in 'asReal'
In addition: Warning message:
In (p - 1):(1) : numerical expression has 20 elements: only the first used

我将如何使用 gmp 包(或类似的包)来执行我创建的循环?

谢谢。

【问题讨论】:

    标签: r loops gmp largenumber


    【解决方案1】:
    install.package("gmp")
    library(gmp)
    factorize(600851475143)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2012-12-18
      • 2011-02-22
      • 2020-07-16
      相关资源
      最近更新 更多