如何解决为什么R不能分配与Python一样多的RAM?还是有不同的解释?
我正在听这个Youtube post,并顺利运行了在GoogleColab上发布的代码:
modprobe
但是当我在R中尝试类似的计算时,我在RStudio和GoogleColab(Google VM)中遇到了大小限制:
import numpy as np
import math
def get_primes(n_min,n_max):
result = []
for x in range(max(n_min,2),n_max):
has_factor = False
for p in range(2,int(np.sqrt(x)) + 1):
if x % p == 0:
has_factor = True
break
if not has_factor:
result.append(x)
return result
get_primes(10**12,10**12+1000)
解决方法
谢谢@Jean_FrançoisFabre在我的脑海里植入了这个bug,让我整个下午都在想他的评论:
它与R无关。不要使用p
是的,一旦我弄清楚如何消除巨大的空向量,一切都很好:
get_primes <- function(n_min,n_max){
options(scipen=999)
result = vector()
if(n_min<=2) result <- c(result,2)
for (x in seq(max(n_min,2),n_max)){
has_factor <- F
for (p in seq(2,ceiling(sqrt(x)))){
if(x %% p == 0) has_factor <- T
if(has_factor == T) break
}
if(has_factor==F) result <- c(result,x)
}
result
}
system.time({them_primes <- get_primes(1e12,1e12+1000)})
# user system elapsed
# 17.41 0.00 17.44
them_primes
[1] 1000000000039 1000000000061 1000000000063 1000000000091 1000000000121
[6] 1000000000163 1000000000169 1000000000177 1000000000189 1000000000193
[11] 1000000000211 1000000000271 1000000000303 1000000000331 1000000000333
[16] 1000000000339 1000000000459 1000000000471 1000000000537 1000000000543
[21] 1000000000547 1000000000561 1000000000609 1000000000661 1000000000669
[26] 1000000000721 1000000000751 1000000000787 1000000000789 1000000000799
[31] 1000000000841 1000000000903 1000000000921 1000000000931 1000000000933
[36] 1000000000949 1000000000997
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。